#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0

    What am I doing wrong?


    I'm trying to code an application that displays this:

    When the zip code matches the city name:
    Enter the City Name
    Lake Grove
    Enter the Zip Code
    11755
    Your Answer is right
    When the zip code does not match the city name:
    Enter the City Name
    Lake Grove
    Enter the Zip Code
    11755
    Your Answer is wrong
    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:
    import java.util.Scanner;

    public class ZipCodeFinder
    {
    public static void main(String[] args)
    {
    // declare boolean variable
    boolean blnAnswer = false;

    // create a Scanner object named sc
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the City Name");
    String strCity = sc.next();
    System.out.println("Enter the Zip Code");
    String strZip = sc.next();

    // switch statements
    switch (strCity)
    {
    case "Lake Grove":
    if (strZip == "11755")
    blnAnswer = true;
    else
    blnAnswer = false;
    break;
    }
    if (blnAnswer == false)
    System.out.println("Your Answer is Wrong");
    else if (blnAnswer == true)
    System.out.println("Your Answer is Right");
    }
    }

    I'm using a switch statement because I plan on adding more zip codes and places to this application when I am done.
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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:
    Code:
    if (strZip.equals("11755"))
    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.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    There are several other issues. You particularly seem to have trouble using booleans.

    The expression
    Code:
    strZip == "11755"
    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:
    Code:
    if ( strCity.equals("Lake Grove") && strZip.equals("11755") )
    	System.out.println("Your Answer is Right");
    else
    	System.out.println("Your Answer is Wrong");

IMN logo majestic logo threadwatch logo seochat tools logo