Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: If else problem

  1. #1
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default If else problem

    I am trying to make it so when "new game" is put in as the user input then the program should return "Hello", but if anything else is entered then "Goodbye" is returned. I am using these as examples until I have fixed this problem, I have played around and tried multiple things and I do not see the problem. Could someone please try to shed some light on the situation?

    Java Code:
    import java.util.Scanner;
    
    public class MainMenu {
    	public static void main(String[] args) {
    		System.out.print("New Game\nLeaderboard\n");
    	
    		String userInput;
    		Scanner sc = new Scanner(System.in);
    		userInput = sc.next();
    		
    		if(!userInput.equalsIgnoreCase("new game")) {
    				System.out.println("Hello");
    		} else {
    				System.err.println("Goodbye");
    		}
    		
    		sc.close();
    	}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: If else problem

    Debugg or print the UserInput to the screen or read the api doc of Scanner.next()!

  3. #3
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    I did have to print userinput to the screen but I wanted to change it to this for the time being.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    I did have to print userinput to the screen
    What was printed? Was it what you expected?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: If else problem

    The problem is at the next() method of the Scanner. It returns a String containing everything up to the first space.
    So if you put in new game only new is returned.

    Use nextLine() instead and it should work fine.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    @JavaAdvisor - Does solving the OPs problem for him teach him techniques he can use to solve future problems?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    Now I am having a problem with the print being backwards so when "new game" is typed it prints Goodbye and vice versa.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    Post the code with the problem.
    Sounds like your logic testing is backwards.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    Java Code:
    import java.util.Scanner;
    
    public class MainMenu {
    	public static void main(String[] args) {
    		System.out.print("New Game\nLeaderboard\n");
    	
    		String userInput;
    		Scanner sc = new Scanner(System.in);
    		userInput = sc.nextLine();
    		
    		if(!userInput.equalsIgnoreCase("new game")) {
    				System.out.println("Hello");
    		} else {
    				System.out.println("Goodbye");
    		}
    		
    		sc.close();
    	}
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    What do you expect the code to print if userInput contains "new game"?

    Can you also post the contents of the console that shows what was printed out and what you entered?
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Jordan>cd desktop

    C:\Users\Jordan\Desktop>cd java

    C:\Users\Jordan\Desktop\Java>cd high low game

    C:\Users\Jordan\Desktop\Java\High Low Game>java MainMenu
    New Game
    Leaderboard
    new game
    Goodbye

    C:\Users\Jordan\Desktop\Java\High Low Game>

  12. #12
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    I expect it to print "Hello"

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    Do you know what the ! operator is and what it does? The code is using it in the if statement.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    No I dont

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    How did it get in the code?

    What happens if you remove it?

    Take a look at the tutorial: Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    Someone gave me help with the code and getting the Input from the user, unfortunately they didn't explain it to me.

  17. #17
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    If I remove the "!" would that work or would it need to be replaced?

    EDIT: Yes, it works, thank you for your help that helped a lot :D

    My next query is about how I can run another class file from inside this class file when the correct input is given.
    Last edited by TANKDS; 09-20-2012 at 09:54 PM.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    To call a method in another class, you need a reference to the class. For example in your code:
    Java Code:
            Scanner sc = new Scanner(System.in);  //  Get instance of Scanner class in sc
            userInput = sc.nextLine();              // Call Scanner's nextLine() method
    It gets a reference to an instance of the Scanner class and calls a method in that class.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    TANKDS is offline Member
    Join Date
    Feb 2012
    Posts
    54
    Rep Power
    0

    Default Re: If else problem

    So when new game is given as the userinput in the mainmenu class will this allow me to run the newgame class?

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: If else problem

    Probably yes. The terminology is "call method", not "run class".
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 04:43 AM
  2. Replies: 9
    Last Post: 09-21-2010, 05:15 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
  •