Vector Method Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to create a vector method called readfromfile which would potentially read the input from a different text file. Why does it give an error?
Edit: Thanks for the help, I have edited the code and it works!
Looks like I was confusing parameters and methods! :P
Thanks guys :D
package cas.lab1.firsteclipsePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
public class FirstEclipseClass {
public static void main(String[] args) {
Vector input = new Vector();
input.add("A");
input.add("B");
input.add("C");
input.add("D");
printVectorElements(input, 3);
Vector<String> results = readFromFile();
}
public static void printVectorElements(Vector input, int count) {
for (int i = 0; i < count; i++) {
System.out.println(input.get(i));
}
}
public static Vector<String> readFromFile(){ //yeah I did confuse methods and parameters
Vector<String> result = new Vector<String>();
try{
File f = new File("input.txt");
Scanner s = new Scanner(f);
while(s.hasNextLine()) {
int i = s.nextInt();
if(i % 2 == 0)
result.add("Even");
else
result.add("Odd");
System.out.println(i);
}
s.close();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
return result;
}
}

I guess that you are confused here. From your method call, I see that you don't need to pass any parameters, and instead want a Vector back. So I suggest you to change this line:
public static readFromFile(Vector<String> results){
To this line:
public static Vector<String> readFromFile(){

First thing: you didn't specified the return type. You should have :
public static Vector<String> readFromFile()
if you do not need any parameters in the function.
Second, for future, you cannot have this same name in the function and as a function parameter

Related

this code of mine has no error but it isnt showing any output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In this code the method setEid isn't working dont know what's the problem with while loop
this code of mine has no error but it isnt showing any output
public class Emp {
private String eid;
public String getEid() {
return eid;
}
public void setEid(String e) {
while (e.length() < 12) {
eid = e;
}
}
}
public class Test {
public static void main(String[] args) {
Emp z = new Emp();
z.setEid("rgrge");
System.out.println("\n" + z.getEid());
}
}
enter code here i expect the static initialization of setEid argument should not take more than 12 characters
Your setter method have a while loop if eid length is less than 12 then it always stuck in this method.
public void setEid(String e){
while(e.length()<12)
eid=e;
}
and In your main method
public static void main(String[] args) {
Emp z=new Emp();
z.setEid("rgrge"); // you call setter
System.out.println("\n"+z.getEid());
}
you pass "rgrge" in setter and its length is less than 12. Tht's why your program is stuck in loop and not showing any thing.
Change setter implementation to this.
public void setEid(String e){
if(e.length()<12) // change while to if
eid=e;
}
so first there should have had if instead of while.
Change:
while(e.length()<12) change this as if(e.length()<12)
Explanation:
your setEid method never going to run because there is no break condition there. your while loop condition tends to the infinite. your code gets stuck in that while loop.
here you passed z.setEid("rgrge");. in your example, Length of string e is 5 which is obviously less than 12, so the condition will always be satisfied and your program will be stuck in infinite loop.

Where is the solution stub in this program and how would I call a method in it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is a specific question, don't downvote it just because it doesn't help you.
public class Answer {
public static String answer(int n) {
String nums="";
int limit = 10005;
int x=2;
while(limit>0){
if(isPrime(x)){
limit-=String.valueOf(x).length();
nums = nums + String.valueOf(x);
}
x+=1;
}
String out="";
if(n==0){
out="23571";
}else{
for(int i=1;i<6;i++){
out += String.valueOf(nums.charAt(n+i));
}
//Problem Solved: instead of this loop, it should be out = nums.substring(n,n+5)
}
return out;
}
public static boolean isPrime(int number) {
for(int check = 2; check < number; ++check) {
if(number % check == 0) {
return false;
}
}
return true;
}
}
Nothing is wrong with this code as far as I know, I'm just using it as an example for you to use.
"It must implement the answer() method in the solution stub." was in the directions for me, but I don't know much about the vocabulary of programming, I only understand logic behind programming, so this is the only thing I don't know how to solve. So what I am asking is where do I put the "answer()" at in this program?
It was looking for substring, which I didn't include because I haven't used java in about a year and simply forgot about it.
Here as I can figure out you have problems in understanding the meaning of "stub". It is simply the test method as provided by the answer here. And if you want to test the above code you have to implement the main method in your code to do the same. Something like this
public static void main(String [] args){
//Either use Scanner object or provide the hard coded input as per your requirements
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(answer(n));
}
EDIT AS PER OP REQUIREMENT
Okay so as per your requirement it is asking you for the unit test. There are many ways to do it but my preferred is to make stub concrete class
Implementation an stub concrete class in JUNIT
class Answer {
public String answer(int n){
// Code body
return "result"// in your case out variable
}
}
class solution extends Answer {
#Override
public String answer(int n){
//return "your stubbed result";
}
}

cannot find symbol - ArrayList; cannot find symbol - Integer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have imported both ArrayList and Integer. In my code I use the symbols in multiple places but I only get an error on one line.
Here are the import statements:
import java.util.ArrayList;
import java.lang.Integer;
Here are the pieces of code that compile properly:
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
private static void addPrime(ArrayList<Integer> primes)
{
int newNumber;
int x;
}
This piece of code return the symbol errors listed above:
while (lastValue < half)
{
addPrime(ArrayList<Integer> primes);
lastValue = primes.get(primes.size()-1);
}
I have done research on the problem but the only answer I can find is that the symbols were not imported properly at the beginning of the program.
A little background if you are not aware of already:
private static void addPrime(ArrayList<Integer> primes)
{
int newNumber;
int x;
}
is the method definition, where we define what the method will do for us. To make it do something for us we call the method passing the arguments, eg. addPrime(argument). Here you got to pass actual argument variable of type which you have defined in method definition.
So addPrime(primes) will work for you.
You don't need to specify the value type when calling a function. So instead of addPrime(ArrayList<Integer> primes); just simply do addPrime(primes);
The addPrime(...) function you have does not do anything in the example you gave.
import java.util.ArrayList;
import java.lang.Integer;
public class test
{
private static ArrayList<Integer> addPrime(ArrayList<Integer> primes, int num)
{
//Seems like you are trying to add primes to the list?
primes.add(num);
return primes;
}
public static void main(String[] args)
{
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
//Usage looks like
primes = addPrime(primes, 13);
// This will return 13 as the number 13 is the 5th element
System.out.println(primes.get(4));
}
}
Coded a bit better would be creating an object class so you do not keep passing around an array list
import java.util.ArrayList;
import java.lang.Integer;
public class IntArrList
{
private ArrayList<Integer> primes;
public IntArrList() {
primes = new ArrayList<Integer>();
}
public void addPrime(int num)
{
primes.add(num);
}
public int getPrimeListValue(int index)
{
return primes.get(index);
}
public ArrayList<Integer> getPrimeList()
{
return primes;
}
}
public class test
{
public static void main(String[] args)
{
IntArrList arrL = new IntArrList();
arrL.addPrime(2);
arrL.addPrime(3);
arrL.addPrime(5);
arrL.addPrime(7);
// This will return 7 as the number 7 is the 4th element
System.out.println(arrL.getPrimeListValue(3));
}
}

Why am i getting NoSuchElementException error in my class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this class who is supposed open a file with a nr of questions in it with a nr of answer.
The problem is when it get's in that for to write in the ArrayList. I have tried all type of ArrayLists,LinkedLists, even vectors.
The first parts of the file, like the question and nr of answer, it takes it without any problem. But when i want to store those answers in a list so i can save that list in an object it won't work.
If anyone could help with this or knows a better method so save an unknown nr of string into an object list it would be epic.
The file format is:
A question,3,yes,no,maybe
Another question,4,yes,maybe,no,why not
my class:
public class GetSurvey {
public static String intrebare;
static int raspunsuri;
public static int i = 1;
static String holder;
//static String[] rasp = new String[250];
static List<String> rasp = new LinkedList<String>();
public static SurveyClass[] obj = new SurveyClass[250];
public static void loadSurvey()
{
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+File.separator+"Survey.dat");
if(!file.exists()){
try {
file.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
if(file.exists()){
try {
Scanner read = new Scanner(file);
read.useDelimiter(",");
while (read.hasNext())
{
intrebare = read.next();
String raspunsuri1 = read.next();
//Log.w("Date",String.valueOf(raspunsuri));
//obj[i].setNrRasp(raspunsuri);
for(int j = 0;j < 3;j++)
{
Log.w("Date",String.valueOf(j));
rasp.add(j,read.next());
}
String[] stringArr = rasp.toArray(new String[rasp.size()]);
Log.w("Date",stringArr[i]);
//obj[i].setRaspunsuri(rasp);
rasp.clear();
i++;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
You're using read.next() 3 times inside your loop, so that means that you get past the end of the input, hence NoSuchElementException.
Use it only once and assign it to a local variable inside the loop, so you can use that value whenever you need it again later in the loop.
I'd suggest you should perform only one read.next() inside your while(read.hasNext()) Loop . Analyse the read element, perform the neccessary task and rerun the loop until while(read.hasNext()) fails. This way you can be sure you'll never perform a read beyond the size of your List.

Why does this array initializer throw a NullPointerException? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am studying Java currently, and I am wondering why this code throws a NullPointerException at the line indicated below. The Question object is a user-defined class that takes two strings as parameters when initializing the object.
public class QuizTime
{
public static void main (String[] args)
{
Quiz qz = new Quiz();
// Throws a NullPointerException
qz.add (new Question ("How may US states are there?", "50"));
}
}
Below is the supporting class. The NullPointerException also indicates a problem with the line "quiz[count] = q;"
import java.util.Scanner;
public class Quiz
{
private Question[] quiz;
private int count;
private final int MAX_QUESTIONS = 25;
public void Quiz ()
{
quiz = new Question[MAX_QUESTIONS];
count = 0;
}
public void add (Question q)
{
if (count < MAX_QUESTIONS)
{
// Throws a NullPointerException
quiz[count] = q;
count++;
}
}
public void Quiz ()
should be
public Quiz()
in order to be considered a constructor and correctly initialize your object. Otherwise, it is a method which you haven't invoked.

Categories