Results 1 to 13 of 13
Thread: How would I code this
- 02-15-2011, 02:52 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
How would I code this
Instructions are as follows:
I may be going about this incorrectly, but I have tried a few different options and have found them all to fail in some way.The user must enter either "4", "5", "9", or "11". If any other number is entered, the program will print out "Invalid choice." and will terminate with no further output.
Java Code:if (userInput != "4" && userInput != "5" && userInput != "9" && userInput != "11") { System.out.println("Invalid Choice. "); } else { System.out.println("Which sport has "+userInput+" players? "); }Java Code:if (userInput == "4" || userInput == "5" || userInput == "9" || userInput == "11") { System.out.println("Which sport has "+userInput+" players? "); } else { System.out.println("Invalid Choice. "); }Java Code:if (userInput == "4") { System.out.println("Which sport has "+userInput+" players? "); } if (userInput == "5") { System.out.println("Which sport has "+userInput+" players? "); } if (userInput == "9") { System.out.println("Which sport has "+userInput+" players? "); } if (userInput == "11") { System.out.println("Which sport has "+userInput+" players? "); } else if (userInput != "4" && userInput != "5" && userInput != "9" && userInput != "11") { System.out.println("Invalid Choice. "); }Java Code:if (userInput.equals("4")) { System.out.println("Which sport has "+userInput+" players? "); } if (userInput.equals("5")) { System.out.println("Which sport has "+userInput+" players? "); } if (userInput.equals("9")) { System.out.println("Which sport has "+userInput+" players? "); } if (userInput.equals("11")) { System.out.println("Which sport has "+userInput+" players? "); } if (userInput != "4" || userInput != "5" || userInput != "9" || userInput != "11") { System.out.println("Invalid Choice. "); }
None of these have worked correctly. I can paste my entire code if necessary. What I want to happen is if the input is 4,5,9, or 11, the program outputs the "Which sport has.." line, and if it is any other number it outputs "Invalid Choice." However, when I run the code with a valid number (4) in the console it will print "Which sport has 4 players?" but also directly under it, it will still print "invalid choice."
Any help is appreciated.
- 02-15-2011, 03:15 AM #2
When comparing Objects (Strings are objects) 99.99999% of the time you do not want to use == but use the equals method instead. That is why the first three atempts do not work. What about your fourth attemp? That uses the equals method. Provide more information about what "doesn't work" means.
- 02-15-2011, 03:16 AM #3
Ahhh, I just realised that after using the equals methods several times you revert back to using == in the last if statement.
- 02-15-2011, 03:23 AM #4
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
I think your logic is off in atleast a couple of the examples but I would imagine the real problem you are having is that you are comparing Object references and not Strings. Look up the String CompareTo(), Equals(), and Compare(). Java: ==, .equals(), compareTo(), and compare()
- 02-15-2011, 03:24 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Yeah at first instead of the string userInput I just assigned the number as int n, then when it didn't work tried to change it to a string.
When you say revert back, where are you referring to? When I do "!="?
Is there a way to code "not equal to" in the equals method and not using "!="?
- 02-15-2011, 03:25 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
- 02-15-2011, 03:42 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Gonna try to add some more information. I have concluded the last one is my best best so far. So I am using it. I don't think the error is with the != but I could be wrong. The entire program was working as required except I missed the instruction that if 4,5,9, or 11 wasn't input, it had to say invalid choice.
The problem is, after I input 4 it will say "What sport has 4 players?" but also in the next line say "invalid choice". I don't want the invalid choice to come up when a valid number of players is entered.
- 02-15-2011, 03:51 AM #8
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
//This is your first example
I have not tested this code but I think the logic should look like this
I think the logic you are looking for isJava Code:if (userInput.equals("4") || userInput.equals("5") || userInput.equals("9") || userInput.equals("11") { System.out.println("Which sport has "+userInput+" players? "); } else { System.out.println("Invalid Choice. "); }
If input = somevalue OR
input = someOtherValue
THEN do this
ELSE bad input
- 02-15-2011, 04:15 AM #9
In your first post, 4th approach you are using ONLY if statements. Then you are using == / != to compare Strings in the last if statement. That shouldn't be the preferred way when you are comparing objects. Always use equals() for better results.
The same thing which I said above. Use else statements,
See the approach yellowledbet has shown above.Java Code:if(x.equals("x")){ //Equal to logic } else { //Not equal to logic }
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-15-2011, 04:35 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Thanks guys, gonna try these after I'm done eating.
- 02-15-2011, 04:36 AM #11
- 02-15-2011, 04:47 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
It worked guys, thanks. I repped you all, hope it helps.
- 02-15-2011, 06:16 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Java Code:if (userInput != "4" || userInput != "5" || userInput != "9" || userInput != "11") { System.out.println("Invalid Choice. "); }
Not that you need it now, but you will at some point: what you meant there was
Java Code:if (!userInput.equals("4") [b]&&[/b] !userInput.equals("5") [b]&&[/b] !userInput.equals("9") [b]&&[/b] !userInput.equals("11")) { System.out.println("Invalid Choice. "); }
Confusingly we say "not a or b" when we mean "not a and also not b".
Similar Threads
-
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks