reading files and arraylists
Code:
ArrayList<text> words = new ArrayList<text>();
while(in.hasMoreTokens()){
words.add( new text(in.readToken().toLowerCase()));
}
i'm reading tokens from a .txt and adding them to this arraylist
however, when i go and print, each instance is the same.
any ideas?
edit: it's a problem with assigning strings to each other.
in the text class constructor i assign the word to another string, and i guess it only assigns the reference.
how do i fix it?
Code:
public text(String w){
word = w.toString();
occurance =1;
}
Text class should look like...
You don't show your Text class, but it should look something like this.
Code:
public class Text {
private final String _Word;
public Text(final String pWord) {
_Word = pWord;
}
public String getWord() {
return _Word;
}
The constructor needs to store the String passed in an instance variable, and a getter method provides access to the String.