February 20th, 2013, 07:51 AM
What am I doing wrong?
I'm trying to code an application that displays this:
When the zip code matches the city name:
When the zip code does not match the city name:
Also, another thing I noticed is that whenever I enter a space between "lake" and "grove", the application automatically says "BUILD SUCCESSFUL" without me even being able to put in the zip code. How can I change that?
My current code:
I'm using a switch statement because I plan on adding more zip codes and places to this application when I am done.
February 20th, 2013, 08:11 AM
1) Your comparison won't work because you're not comparing String objects in the right way. The == operator should only be used with primitive values if you're checking for equality (int, double, float, boolean, etc.). For objects, use the .equals method, like so:
2) The .next method of a scanner reads the next token. By default, a 'token' is everything up to the next space, so that's why it only reads the first word of your street name. Consider using .nextLine instead.
February 20th, 2013, 08:59 AM
There are several other issues. You particularly seem to have trouble using booleans.
already evaluates to either true or false, so there is no point in putting that into an if statement to retrieve the result of this check. Just assign the expression to blnAnswer.
There's also absolutely no point in comparing a boolean to true or false. A boolean already is either true or false. It doesn't get "truer" or "falser". And what's the point of "if (false) else if (true)"? What other options do you expect? Maybe FileNotFound?
You could narrow down the code to a few lines if you simply check both values:
if ( strCity.equals("Lake Grove") && strZip.equals("11755") )
System.out.println("Your Answer is Right");
System.out.println("Your Answer is Wrong");