NoSuchElementException Error sentinel Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimgym1989
    New Member
    • Sep 2008
    • 30

    NoSuchElementException Error sentinel Loop

    Code:
     import java.io.*;
    import java.util.*;
    public class tryCountLetter {
    
        public static void main(String[]args) throws FileNotFoundException{
        	
        	Scanner inFile = new Scanner(new FileReader("D:\\My Documents\\Java Saves\\try.txt"));
    	
    	int counter = 0;
    		
    	String str=inFile.next();
    		while(str!= "-999")
    		{
    		str = inFile.next();
    		System.out.print(str.length());
    		}
    		inFile.close();
    	}
           
        }
    this is the error

    Exception in thread "main" java.util.NoSuc hElementExcepti on
    at java.util.Scann er.throwFor(Sca nner.java:817)
    at java.util.Scann er.next(Scanner .java:1317)
    at tryCountLetter. main(tryCountLe tter.java:22)

    i dont get it?
    why does it say NoSuchElementEx ception?

    this is what's inside of the try.txt

    Im a newbie -999
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You can't compare strings for (in)equality like you did in line twelve. Do this
    instead:

    [code=java]
    while (!str.equals("-999"))
    [/code]

    kind regards,

    Jos

    Comment

    • jimgym1989
      New Member
      • Sep 2008
      • 30

      #3
      Originally posted by JosAH
      You can't compare strings for (in)equality like you did in line twelve. Do this
      instead:

      [code=java]
      while (!str.equals("-999"))
      [/code]

      kind regards,

      Jos
      I still received the same error..
      I don't get it..
      thank you for you immediate response!!

      Comment

      • jimgym1989
        New Member
        • Sep 2008
        • 30

        #4
        Originally posted by JosAH
        You can't compare strings for (in)equality like you did in line twelve. Do this
        instead:

        [code=java]
        while (!str.equals("-999"))
        [/code]

        kind regards,

        Jos
        by the way i already solved it..thank any way :D

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by jimgym1989
          by the way i already solved it..thank any way :D
          So how did you solve it?

          kind regards,

          Jos

          Comment

          Working...