• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loop Problems

 
Chris Addison
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!

HTH
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic