permaculture playing cards*
The moose likes Beginning Java and the fly likes Why is my loop not working properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is my loop not working properly?" Watch "Why is my loop not working properly?" New topic
Author

Why is my loop not working properly?

E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
I am having a problem processing some input from an input file.
I have a loop that goes through the file line by line and when it encounters
"-911" it will quit looking through the file. (-911 indicates the end of the file. To do this I tried using:
String lineIn = in.readLine();
while (lineIn != "-911"){
// code for processing input
lineIn = in.readLine();
}
So when lineIn is -911 it should not go back into the loop. Well even when it is -911 it goes through the loop once again and I get a NoSuchElementException (from trying to read null I assume). I also placed a few traces in at the beginning and end of the loop to see what lineIn is. On the time before I get the exception it equals -911 but it still goes into the loop. Why is this loop not working?
Here is the complete loop:
String lineIn = in.readLine();
while (lineIn != "-911"){lineIn = lineIn.trim();
int ok = 0;
StringTokenizer maximum = new StringTokenizer(maxVals);
StringTokenizer keys = new StringTokenizer(lineIn);
while (maximum.hasMoreTokens()) {
for (int amount = L2; amount > 0; amount--);{String val = keys.nextToken();int keyval = Integer.parseInt(val.trim());String max = maximum.nextToken();
int maxi = Integer.parseInt(max.trim());
if (keyval <= maxi) { ok = ok +1; }}
if ( ok == L2){
if (!(hashTable.contains(lineIn))){ insertIt(lineIn, hashTable);}
}
}
lineIn = in.readLine();
}
Thanks for any help.
E Fox
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
This is one of the first errors I ever encountered in Java programming (and I have made it a few times since then ). Since lineIn is an object (of type String), you can't compare it to another String by using the == (or !=) operators. That's because with objects the equality operators check to see that the objects are exactly the same, not just that their contents are the same. (I hope this isn't too confusing a sentence; if it's not clear, let me know and I'll try to explain in more detail.)
Anyway, the way out of your problem is to use the equals() method. Try:
while (lineIn.equals("-911") == false) {
The equals() method compares the contents of lineIn to the value '-911', and returns true if they are equal. If this method returns false, the two strings are not the same.
Joe
E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
I tried it but it does the same thing. The complete error is:
Exception in thread "main" java.util.NoSuchElementException
at hava.util.StringTokenizer.nextToken(StringTokenizer.java:232)
at hash2.main(hash2.java:51)
Seems to have something to do with my use of StringTokenizer, the loop posted above has all the instances of StringTokenizer that are in my program. Any ideas?
Thanks
E Fox
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
You said before that the program still entered the loop even after reading the value "-911". Did you check to see whether you fixed that problem? Or is the program still entering the loop even after receiving the end-of-file condition?
Joe
E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
yes it still enters the loop after getting -911.
Roy Tock
Ranch Hand

Joined: Jul 16, 2001
Posts: 83
Hey, E, maybe the "-911" line in the text file contains some trailing whitespace. If it does, your current code will fail. You can catch the problem in one of two ways:
1. Use "startsWith", rather than "equals", like this:

2. Trim the string before trying .equals, like this:
E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
Thank you so much, that worked.
E Fox
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is my loop not working properly?