Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Loop Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop Problems" Watch "Loop Problems" New topic

Loop Problems

Chris Addison

Joined: May 15, 2011
Posts: 1
Hi guys,

I'm new here and basically I really need some help, I'm trying to validate an if statement (see below) so it will return true and complete the loop is the type of car picked up is a commercial car or a private car, I'm parsing from a .txt file and assigning the data to a local variable called cartype then creating a new instance of either a public or commercial vehicle. The problem is that the program keeps running through the loops without recognising the return should equal true and skimming right over them both. Could you please help!

Kacper Jonczyk

Joined: Aug 25, 2010
Posts: 9
Hi Chris,

I'd start with changing comparison operator '==' with equals() method. Afterwards, if problem still persists You may want to start debugging this program to see, if cartype variable holds the value You're looking for.

Welcome to JavaRanch !!!

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
You should not be comparing String values using the == operator, instead use equals() method.

Im not sure if I understand your problem, is the loop not ending? If so, try printing out the value of s in each pass, maybe the TextReader is at fault?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46328
If you still have problems, try changing the TextReader to a Scanner, unless you have been specifically told to use that class. I can't see anything wrong with your loop. It is most likely a problem with the use of ==. You must be splitting the string successfully, otherwise the invocation of vehicle[1] would cause an Exception.
I agree. Here's the link:
subject: Loop Problems
It's not a secret anymore!