Results 1 to 3 of 3
  1. #1
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default reading files and arraylists

    Java 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?

    Java Code:
    	public text(String w){
    		
    		word = w.toString();
    	
    		occurance =1;
    	}
    Last edited by infinity; 01-08-2009 at 09:23 PM.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    10

    Default Text class should look like...

    You don't show your Text class, but it should look something like this.
    Java 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.

  3. #3
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 04:15 AM
  2. Reading growing files
    By ionna22 in forum Advanced Java
    Replies: 0
    Last Post: 10-20-2008, 04:43 PM
  3. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •