Results 1 to 5 of 5
Thread: A little help please
- 04-21-2011, 05:59 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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
-
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:
Best of luckJava Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 04-21-2011, 06:21 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Thanks for your help I now know what the problem is, I just need to learn how to fix it.
- 04-21-2011, 06:50 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re read fubars post, he gave you two methods to use to compare strings.
- 04-21-2011, 07:57 PM #5
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
corrected code using the .equals method; always use the .equals method while comparing string never (==)
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(); if (state.equals("WA")) { System.out.print("Washington"); } else { System.out.print("please try again"); } } }
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.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks