Results 1 to 13 of 13
  1. #1
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default How would I code this

    Instructions are as follows:

    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.
    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.

    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Ahhh, I just realised that after using the equals methods several times you revert back to using == in the last if statement.

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    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()

  5. #5
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Ahhh, I just realised that after using the equals methods several times you revert back to using == in the last if statement.
    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 "!="?

  6. #6
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by yellowledbet View Post
    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()
    Thanks, taking a look at the site now.

  7. #7
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  8. #8
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by iCon09 View Post
    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.
    //This is your first example

    I have not tested this code but I think the logic should look like this
    Java 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. ");
    					}
    I think the logic you are looking for is

    If input = somevalue OR
    input = someOtherValue
    THEN do this
    ELSE bad input

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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.

    Quote Originally Posted by iCon09 View Post
    Is there a way to code "not equal to" in the equals method and not using "!="?
    The same thing which I said above. Use else statements,
    Java Code:
    if(x.equals("x")){
           //Equal to logic
          } else {
           //Not equal to logic
         }
    See the approach yellowledbet has shown above.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks guys, gonna try these after I'm done eating.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by iCon09 View Post
    When you say revert back, where are you referring to? When I do "!="?
    What I meant was that you were using the equals method in the first few if statements then in the last you went back to using == or !=, as already explained this is wrong.

    If you want to use not equals method then the syntax is:
    Java Code:
    if ( ! str1.equals(str2)) {
    
    }

  12. #12
    iCon09 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    It worked guys, thanks. I repped you all, hope it helps.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •