Connecting Tech Pros Worldwide Forums | Help | Site Map

NoSuchElementException Error sentinel Loop

Newbie
 
Join Date: Sep 2008
Posts: 27
#1: Sep 18 '08
Expand|Select|Wrap|Line Numbers
  1.  import java.io.*;
  2. import java.util.*;
  3. public class tryCountLetter {
  4.  
  5.     public static void main(String[]args) throws FileNotFoundException{
  6.  
  7.         Scanner inFile = new Scanner(new FileReader("D:\\My Documents\\Java Saves\\try.txt"));
  8.  
  9.     int counter = 0;
  10.  
  11.     String str=inFile.next();
  12.         while(str!= "-999")
  13.         {
  14.         str = inFile.next();
  15.         System.out.print(str.length());
  16.         }
  17.         inFile.close();
  18.     }
  19.  
  20.     }
this is the error

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1317)
at tryCountLetter.main(tryCountLetter.java:22)

i dont get it?
why does it say NoSuchElementException?

this is what's inside of the try.txt

Im a newbie -999

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Sep 18 '08

re: NoSuchElementException Error sentinel Loop


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

Expand|Select|Wrap|Line Numbers
  1. while (!str.equals("-999"))
  2.  
kind regards,

Jos
Newbie
 
Join Date: Sep 2008
Posts: 27
#3: Sep 18 '08

re: NoSuchElementException Error sentinel Loop


Quote:

Originally Posted by JosAH

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

Expand|Select|Wrap|Line Numbers
  1. while (!str.equals("-999"))
  2.  
kind regards,

Jos

I still received the same error..
I don't get it..
thank you for you immediate response!!
Newbie
 
Join Date: Sep 2008
Posts: 27
#4: Sep 18 '08

re: NoSuchElementException Error sentinel Loop


Quote:

Originally Posted by JosAH

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

Expand|Select|Wrap|Line Numbers
  1. while (!str.equals("-999"))
  2.  
kind regards,

Jos

by the way i already solved it..thank any way :D
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Sep 18 '08

re: NoSuchElementException Error sentinel Loop


Quote:

Originally Posted by jimgym1989

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

So how did you solve it?

kind regards,

Jos
Reply