Iterator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    Iterator

    Hi, when i call this method from another class, all im getting is null (value of NONE_LEFT) but i have tested the wordCollection hashset with a for each and it does contain Strings, plenty of them!

    Code:
        public String getNextWord()
        {
            if(iter.hasNext())
            {
                
                return iter.next();
            }
            else
            {
                
                return NONE_LEFT;
            }    
                
        }
    Any one see why without having to see any more of my code?

    just for good measure...
    Code:
    private HashSet<String> wordCollection = new HashSet<String>();
        private Iterator<String> iter = wordCollection.iterator();
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by brendanmcdonagh
    Hi, when i call this method from another class, all im getting is null (value of NONE_LEFT) but i have tested the wordCollection hashset with a for each and it does contain Strings, plenty of them!

    Code:
        public String getNextWord()
        {
            if(iter.hasNext())
            {
                
                return iter.next();
            }
            else
            {
                
                return NONE_LEFT;
            }    
                
        }
    Any one see why without having to see any more of my code?

    just for good measure...
    Code:
    private HashSet<String> wordCollection = new HashSet<String>();
        private Iterator<String> iter = wordCollection.iterator();
    Will you post the class that holds the given method?
    and also the snippet where the instance have been used to access that method....

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      Hi, heres the code for the calling class

      Code:
      /*
       * TestWordReader.java
       *
       * 2008J TMA01 Question 4
       */
      
      package tma01q4;
      
      public class TestWordReader
      {
          
          public static void main(String args[])
          {
              try
              {
              String emptyFile = "emptyfile.txt";
              String wordList  = "wordlist.txt";
              String shortFile = "shortWordlist.txt";
              
              
              //WordReader emptyTest = new WordReader(emptyFile);
              WordReader wordListTest = new WordReader(wordList);
              //WordReader shortTest = new WordReader(shortFile);
              
              //2. For the selected WordReader, attempt to read and display the first 
              //   five words in its word collection, using the getNextWord method
              for(int i = 0; i < 5; i++)
              {
                  String aString = wordListTest.getNextWord();
                  System.out.println(aString);
              }
              }
          
          catch (Exception anException)
          {
              System.out.println("Error " + anException);
          }
          }
      }
      Don't understand what you mean when you say snippet

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        From your code above, the Iterator iter is constructed from an empty wordCollection. Add the Strings to the wordCollection before creating the iterator.

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #5
          Thank you so much for helping, as soon as you said it my understanding changed forever!

          However, i ve upadate my constructer and now i get a nullpointerexce ption but nothing but the constructer has changed and using the for each the words are there??

          Code:
          public HashSet<String> wordCollection;
              public Iterator<String> iter;
              
              private static final String NONE_LEFT = null;
              
              //complete the constructor
              public WordReader(String f) throws FileNotFoundException, EmptyFileException
              {
                  
                  Scanner aScanner = new Scanner(fileName = new File(f));
                  while(aScanner.hasNextLine())
                  {
                      String aWord = aScanner.nextLine();
                      wordCollection.add(aWord);
                             
                            
                  }
                  wordCollection = new HashSet<String>();
                  iter = wordCollection.iterator();
              }

          method:

          Code:
              public String getNextWord()
              {
                  if(iter.hasNext())
                  {
                      
                      return iter.next();
                  }
                  else
                  {
                      
                      return NONE_LEFT;
                  }    
                      
              }
          calling method...

          Code:
          
                  for(int i = 0; i < 5; i++)
                  {
                      String aString = wordListTest.getNextWord();
                      System.out.println(aString);
                  }
                  }
          I don't think the way it is written is up to standards but i have to follow guidelines.

          Comment

          • brendanmcdonagh
            New Member
            • Nov 2007
            • 153

            #6
            Got it!!! Thank you so much:)

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              You are trying to add things to wordCollection on line 15 before you have initialized it. You are initializing it later on line 19. Better initialize it as the first line of your constructor.

              Comment

              Working...