Declaring variables in constructor and compiling
Code :
import java.util.*;
class WordStore
{
public WordStore(int n){
int number = n;
}
public String[] storewords = new String[number];
public int count=0;
public void add(String word){
storewords[count] = word;
count=count+1;
}
I am not sure were to declare the String Array storewords, so i just made it public and the same with int count. I tried putting them in the constructor, but it wouldn't recognise the variable.
There are certain conditions I have to meet, like the method add is declared
public void add(String word)
and when add is callled it just stores a word in the array (theres another loop in another class that calls it, and i got to leave it unchanged.
The problem I am getting it doesn't recognise the variable number
Re: Declaring variables in constructor and compiling
I have solved the problem, i declared another integer as public, then made it equal to the constructor argument.
Code :
import java.util.*;
class WordStore
{
public int number = 0;
public WordStore(int n){
number = n;
}
public String[] storewords = new String[number];
public int count=0;
public void add(String word){
storewords[count] = word;
count=count+1;
}
}
Re: Declaring variables in constructor and compiling
Good work for solving your problem ;) I didn't get a chance.
Re: Declaring variables in constructor and compiling
I think private would be even better :)
// Json