Results 1 to 5 of 5
  1. #1
    tacomajames is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default A little help please

    good morning I am trying to write some code that will tell me the state name based on the postal abbreviation using else if statements. My code runs but never provides the correct answer. As of right now I just want it to tell me that the state in question is washington. Why will the program not recognize my input? Any help is appreciated.

    Java Code:
    import java.util.Scanner;
    
    public class Program2Jab
    {
        public static void main (String[] args){
        	
        	String state;
        	Scanner keyboard = new Scanner(System.in);
        	System.out.println("Enter a state abbreviation and");
        	System.out.print("I'll tell you which state it is: ");
        	state = keyboard.nextLine();
        	if (state == "WA")
        		{System.out.print("Washington");
        		}
        	else
        	{System.out.print("please try again");
        		}
        	}
        }
    Last edited by Fubarable; 04-21-2011 at 06:09 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Never compare Strings with ==. Instead use the equals(...) or equalsIgnoreCase(...) method.

    The reason for this is that == compares if one String object is the same object as another String object. You're not interested in that but rather you want to know if the String contained by the String object is the same as the String contained by another String object.


    Also, I added code tags added to your post above to help make it readable.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck

  3. #3
    tacomajames is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your help I now know what the problem is, I just need to learn how to fix it.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Re read fubars post, he gave you two methods to use to compare strings.

  5. #5
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    corrected code using the .equals method; always use the .equals method while comparing string never (==)
    Java Code:
    import java.util.Scanner;
    
    public class Program2Jab
    {
        public static void main (String[] args)
        {
            String state;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a state abbreviation and");
            System.out.print("I'll tell you which state it is: ");
            state = keyboard.nextLine();
            if (state.equals("WA"))
            {
                System.out.print("Washington");
            }
            else
            {
                System.out.print("please try again");
            }
        }
    }
    note that during execution you need to cater for different ways the user enters the state's abbreviation this may be done as follows:
    Java Code:
    import java.util.Scanner;
    
    public class Program2Jab
    {
        public static void main (String[] args)
        {
            String state;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a state abbreviation and");
            System.out.print("I'll tell you which state it is: ");
            state = keyboard.nextLine();
            String state2 = state.toUpperCase();
            if (state2.equals("WA"))
            {
                System.out.print("Washington");
            }
            else
            {
                System.out.print("please try again");
            }
        }
    }
    Last edited by farahm; 04-21-2011 at 08:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •