Results 1 to 3 of 3

Thread: Scanner Loop

  1. #1
    Phenomena is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Scanner Loop

    I need to populate a tree list with the information from a text file. Which I'm able to do but some lines have more than one item in them (could be any number of items on the one line) so how do I get scanner to loop through them for only that line?

    for example the file may look like:
    Java Code:
    Character
    Profession
    Pattern Pattern Pattern
    Profession
    Pattern Pattern
    and the tree would be
    Java Code:
    Character
        Profession
            Pattern
            Pattern
            Pattern
        Profession
            Pattern
            Pattern
    what I have is this

    Java Code:
    	    while(input.hasNext()) {
    	    	characterName = input.nextLine();
    	    	character = new DefaultMutableTreeNode(characterName);
    		    top.add(character);
    		    
    		    prof = input.nextLine();
    		    profession = new DefaultMutableTreeNode(prof);
    		    character.add(profession);
    		
    		    while(theres another pattern on the line()) {
    				pat = input.next();
    				pattern = new DefaultMutableTreeNode(pat);
    				profession.add(pattern);
    		    }
    			
    		    prof = input.nextLine();
    		    profession = new DefaultMutableTreeNode(prof);
    		    character.add(profession);
    		    
    		    while(theres another pattern on the line()) {
    				pat = input.next();
    				pattern = new DefaultMutableTreeNode(pat);
    				profession.add(pattern);
    		    }
    	    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You could use split to get more than one item from a line.
    Java Code:
    Scanner s = new Scanner(//your file);
    while(s.hasNext()) {
      String line = s.nextLine();
      String[] items = line.split(" "); //omits whitespaces
      for(int i = 0; i < items.length; i++)
         add(items[i]); //your method for adding strings
    }
    Now, I'm not sure about this, I'd have to look at Scanners API, but i think you could also just use Scanners next() method, from a line "one two three" it should return one, then two, then three, but as I said, I'm not sure.

  3. #3
    Phenomena is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:13 PM
  2. Problem with while loop and the scanner method
    By hhh80p in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 11:47 AM
  3. while loop bypasses scanner input on 2nd pass
    By xf021209 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 08:10 AM
  4. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 12:06 AM
  5. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:15 AM

Posting Permissions

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