Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Help me please with this

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help me please with this

    So, hello guys, i am trying to make something, and i want to request input from user and it shoud look like this

    1. xxx -> press 1 to choose this
    2. xxx -> press 2 to choose this

    So if they enter 3 or string or whatever i want to restart method and show again

    1. xxx -> press 1 to choose this
    2. xxx -> press 2 to choose this

    So here is method that i am trying to restart

    public static void askUser(){
            System.out.println("xxx");
            System.out.println("1. xxx");
            System.out.println("2. xxx");
            System.out.println("3. xxx");
     
     
            if(userInput.hasNextInt()){
                int numEntered = userInput.nextInt();
     
                    if(numEntered == 1){
                        xx= new xx();
                    } else if(numEntered == 2){
                        gs = new xx();
                    } else if(numEntered == 3){
                        System.exit(0);
                    } 
            } else{
                askUser();
            }
     
     
        }

    So this function i'am calling into main method later, so if anyone could help me that would be cool
    If i try to make it public void than it say can't call non-static methods inside static(main)
    if i try to put it into new class and then call it after i fail input it goes into infinite loop

    Help please.
    Thanks in advance!

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help me please with this

    Using a recursive method to accomplish this is an interesting approach, and I'd like to see you get it working, but you won't (I think). Instead, use a while or do/while loop in main() that calls the method until the correct response is obtained.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with this

    Hello thanks for reply, so i made this into main method

    do {
                askUser();
            } while (userInput.hasNextInt());

    But i cant make it work(i am beginner keep that in mind), so it would be nice if you give me some more guidance.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help me please with this

    But i cant make it work(i am beginner keep that in mind), so it would be nice if you give me some more guidance.
    I recommend you post your updated code, and ask a specific question about it. We can often only guess what you mean by 'I can't make it work' unless you state the problem explicitly.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with this

    Ok so, basically i am trying to make text based game.
    So this should be first menu and then.
    If user type 1 it opens up(GameEnglish) if type 2(GameSerbian) if type 3 Game Exits.
    So i just wanted to check if user type something stupid like string or 4 or whatever number than i want to show this again
    But i can not make it work. Thats my problem. So yeah, i hope you undrestand now.
    I made alot more of this game but i just can make this stupid thing to work.

    So here is code of class:

     
    package com.textgame.Game;
     
    import java.util.Scanner;
     
    public class LanguageChooser {
     
        static Scanner userInput = new Scanner(System.in);
     
        private static GameEnglish ge;
        private static GameSerbian gs;
     
        public static void drawLines() {
            int k = 1;
            while (k < 200) {
                System.out.print("-");
                k++;
            }
        }
     
        public static void askUser() {
     
            System.out.println("Choose Language"); /*  SHOW THIS AGAIN  */
            System.out.println("1. English");/*  SHOW THIS AGAIN  */
            System.out.println("2. Serbian");/*  SHOW THIS AGAIN  */
            System.out.println("3. Exit Game");/*  SHOW THIS AGAIN  */
     
            if(userInput.hasNextInt()){
                int numEnter = userInput.nextInt();
     
                if (numEnter == 1) {
                    ge = new GameEnglish();
                } else if (numEnter == 2) {
                    gs = new GameSerbian();
                } else if (numEnter == 3) {
                    System.exit(0);
                }
            }
        }
     
        public static void main(String[] args) {
            drawLines();
            do {
                askUser();
            } while (userInput.hasNextInt() != true);
     
        }
    }

  6. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help me please with this

    In what way "won't it work?" is it sat in a loop? Are you getting
    compile or runtime errors?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with this

    Ok so with code i posted above when i type something random, it just sat into infinite loop.

    Again, thank you for answering, and for sharing knowledge with other for free, i really appreciate it.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help me please with this

    The basic form of what you need to do is
    boolean userResponseIsWrong = true;
     
    while( userResponseIsWrong )
    {
        // get user response
     
        // evaluate user response, set:
        //     userResponseIsWrong = false if response valid
    }
    The "get user response" part of the code can be in the while() loop or in another method that returns the response, or a couple other variations I can think of. Stick with the most basic approach you can think of and post that when you get it roughed out and need help or show us when it's working and we'll cheer you on.

  9. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help me please with this

    Ok man, i am sorry i am bit tired and all of that so i comed out with this code:

    package com.textgame.Game;
     
    import java.util.Scanner;
     
    public class LanguageChooser {
     
        static Scanner userInput = new Scanner(System.in);
     
        static int numEnter; 
     
        static boolean isWrong = true;
     
        private static GameEnglish ge;
        private static GameSerbian gs;
        private static int numEntered;
     
     
     
        public static void drawLines() {
            int k = 1;
            while (k < 200) {
                System.out.print("-");
                k++;
            }
        }
     
        public static void askUser() {
     
            System.out.println("\nChoose the language you want game to run");
            System.out.println("1. English");
            System.out.println("2. Serbian");
            System.out.println("3. Exit game");   
        }
     
        public static void getResponse(){
            if (userInput.hasNextInt()) {
                numEnter = userInput.nextInt();
     
                if (numEnter == 1) {
                    ge = new GameEnglish();
                } else if (numEnter == 2) {
                    gs = new GameSerbian();
                } else if (numEnter == 3) {
                    System.exit(0);
                }
            }
        }
     
        public static void main(String[] args) {
            drawLines();
     
            while(isWrong){
                askUser();
                getResponse();
     
                if(userInput.hasNextInt() == true){
                        getResponse();
                } else {
                    System.out.println("Enter number: ");
     
                    askUser();
                    getResponse();
                    numEntered = userInput.nextInt();
                } 
            }
            isWrong = false;
     
     
     
     
        }
    }

    So i made it display again but i get java.util.InputMismatchException
    Where i made mistake?

    Thanks and sorry for so much bothering.

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help me please with this

    I did not run this program but something stood out. Check out my comments below in main and the getResponse method.

    package com.textgame.Game;
     
    import java.util.Scanner;
     
    public class LanguageChooser {
     
        static Scanner userInput = new Scanner(System.in);
     
        static int numEnter; 
     
        static boolean isWrong = true;
     
        private static GameEnglish ge;
        private static GameSerbian gs;
        private static int numEntered;
     
     
     
        public static void drawLines() {
            int k = 1;
            while (k < 200) {
                System.out.print("-");
                k++;
            }
        }
     
        public static void askUser() {
     
            System.out.println("\nChoose the language you want game to run");
            System.out.println("1. English");
            System.out.println("2. Serbian");
            System.out.println("3. Exit game");   
        }
     
        public static void getResponse(){
     //does the below line ever get called? Where is it getting input from?
            if (userInput.hasNextInt()) {         
                numEnter = userInput.nextInt();
     
                if (numEnter == 1) {
                    ge = new GameEnglish();
                } else if (numEnter == 2) {
                    gs = new GameSerbian();
                } else if (numEnter == 3) {
                    System.exit(0);
                }
            }
        }
     
        public static void main(String[] args) {
            drawLines();
     
            while(isWrong){
                askUser();
                getResponse();
     
                if(userInput.hasNextInt() == true){
                        getResponse();
                } else {
                    /*I did not run this program but I think this line one should run
                     but where is it getting its input from?*/
                    System.out.println("Enter number: ");   
     
                    askUser();
                    getResponse();
                    numEntered = userInput.nextInt();
                } 
            }
            isWrong = false;
     
     
     
     
        }
    }

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help me please with this

    InputMismatchException occurs when when the Scanner tries to read an int and gets a String or tries to read a double and gets an int etc.

    The problem with the nextInt method is that it does not read new lines/ carriage returns if it doesn't need to. For example if user inputs 9[enter], the nextInt method will only read the 9 and leave the CRLF in the buffer. So the next time you call nextInt it does read the CRLF and returns an empty string which is not an int hence the error. Solution: add a call to nextLine and do nothing with the returned value.
    Improving the world one idiot at a time!

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help me please with this

    An InputMismatchException will occur while running your code when an uncooperative users enters an incorrect response. The program is expecting numeric input, 1 - 3, so typing non-numeric (non-integer) input will generate the error you've reported. Your program should account for incorrect input of this type if it's part of the assignment.

    Once I make your program runnable by removing classes I don't have and adding print statements to reflect the user's choice, I get a "symbol not found" error for numEntered in the main() method. That's the error a cooperative user should be getting. I believe numEntered is a typo, but I'm going to eliminate it anyway.

    Your main() method can be improved:

    - isWrong is only needed as a local variable to main().
    - askUser() could be named more appropriately, like "presentMenu()".
    - getResponse() should return a response that is used by main().
    - getResponse() should return a VALID response that is used by main().
    - if getResponse() is required to return a VALID response, it should contain the while() statement and error messages that collects the user's response until it is correct.
    - if getResponse() does as described above, the while() loop condition in main() will repeat until the user selects '3' for exit. A while( true ) loop would work fine for that.
    - if getResponse() does as described above, the if()/else in main() branches based on the user's response.
    - in main(), the askUser()/getResponse() pair should only occur once.
    - the Scanner object, userInput, should not be used in main().

    and, finally:

    - finer point: if ( aBoolean == true ) is unnecessary. simply if ( aBoolean ) will do.

    Picking and choosing from the above options, I crafted the main() method:
        // a main() method to get the user's menu choice and
        // branch accordingly
        public static void main(String[] args)
        {
            drawLines();
     
            // a loop to present the menu, get user's response, and
            // branch on the user's choice until exit (or any other
            // response) is chosen
            while( true )
            {
                presentMenu();
                int response = getResponse();
     
                // this portion could be another method
                // (note how this code is written so that anyone
                // can run it - no external classes required.)
                if (response == 1) {
                    System.out.println( "Game English" );
                } else if (response == 2) {
                    System.out.println( "Game Serbian" );
                } else {
                    System.exit(0);
                }
            }
     
        } // end method main()
    Good luck! Keep coding.