Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By Tolls
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Problem with detecting input

  1. #1
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Problem with detecting input

    I want the usert to enter a 4 digit integer number, and give them the option to quit the game and display history of guesses. If it is not an integer that is entered prompt user to enter an int, if an int is entered but it is not 4 digits prompt to enter 4 digits. If it is a string that is entered prompt user to enter 4 digit int.
    It works fine if i enter say a 2 digit integer (it prompts me to enter a 4 digit int) however if I enter a string first nothing happens, it does not pick it up, i have to enter the twice before it prompts for me to enter a 4 digit int.

    'keyboard' is instance of Scanner Class - Scanner keyboard = new Scanner(System.in);

    Thanks a milion for any help


    Java Code:
    do
          {
    
             //execute if user input is not an integer;
             while (!(keyboard.hasNextInt()))
             {
                //execute if user selects quit option;
                if  (keyboard.next().equals("quit"))
                {
                   //display good-bye message;
                   Interface.exitGame();
    
                   //exit the program;
                   System.exit(0);
    
                } //end of if-statement;
    
    
                //execute of user selects history option;
                if (keyboard.next().equals("history"))
                {
                   //call method to display previous guesses to user;
                   Interface.displayHistory();
    
                }//end of else-if statement;
    
    
    
                //execute the following if the word 'quit' or 'history' is not typed in;
                else
                {
                   //prompt user to enter an integer value;
                   Interface.IncorrectInt();
    
                }//end of if-else statement;
    
    
             }//end of nested-while loop;
    
    
    
    
             //assign user input to userGuess if it is an integer value;
             userGuess = keyboard.next();
    
    
    
    
             // execute userGuess is less than 4;
             while ((userGuess.length() < 4))
             {
                //prompt user to enter a 4 digit number;
                Interface.IncorrectInt();
    
                //iterate nested-while-loop until the number entered is an integer;
                while (!(keyboard.hasNextInt()))
                {
    
                   //execute if user selects quit option;
                   if  (keyboard.next().equals("quit"))
                   {
                      //display good-bye message;
                      Interface.exitGame();
    
                      //exit the program;
                      System.exit(0);
    
                   } //end of if-statement;
    
                   //execute the following if the word 'quit' or 'history' is not typed in;
                   else
                   {
                      //prompt user to enter an integer value;
                      Interface.IncorrectInt();
                   } //end of if-else statement;
    
                } //end of nested-while loop;
    
                //assign user input to userGuess;
                userGuess = keyboard.next();
    
             } // end of while-loop;
    Last edited by ConnorSG1; 11-20-2012 at 04:45 PM. Reason: change description

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Problem with detecting input

    What is 'keyboard'?
    ConnorSG1 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Re: Problem with detecting input

    sorry keyboard is instance of Scanner Class - Scanner keyboard = new Scanner(System.in);

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Problem with detecting input

    You have lots of cases where you call keyboard.next() and simply check the result, followed by another call to it and checking that result, with no further prompt in between.
    eg
    Java Code:
    if  (keyboard.next().equals("quit"))  {
    ...
    } 
    //end of if-statement;
    if (keyboard.next().equals("history")) { 
    ...
    }
    //end of else-if statement;
    Since these are options for the user to take, I would expect you to read once into a variable then test the variable.
    In the above case what will happen is, if the use has not entered 'quit' the system will sit there and wait for them to enter something else nefore checking that against 'history'.
    That's the first thing I can see.

    Now, if that's happening for that case, then I suspect you are doing a similar thing for the non-int case.

    I can't actually see the flow terribly well as there is an awful lot of whitespace in there.
    ConnorSG1 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Re: Problem with detecting input

    Thanks alot, i did as you said, read once into a variable and then test the variable. The program now works perfectly.

    Sorry one more thing, at Uni they tell me the more spaces the better as the program is easier to see, but you do seem to think its not a good thing. Which should i do lol?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Problem with detecting input

    Whitespace is good for indenting, which you've done.
    Looking again, it's not so much the blank lines (though there are a few double blanks up there), but the comment for every line of code that's throwing me.
    ConnorSG1 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Detecting buttons
    By jsobel in forum AWT / Swing
    Replies: 2
    Last Post: 04-06-2012, 05:52 PM
  2. Detecting Capitalization
    By danthegreat in forum New To Java
    Replies: 10
    Last Post: 09-09-2011, 09:46 PM
  3. Detecting if string or int
    By Alk in forum New To Java
    Replies: 6
    Last Post: 05-14-2011, 11:26 PM
  4. Input problem
    By Sotsiak in forum New To Java
    Replies: 1
    Last Post: 06-04-2010, 01:07 PM
  5. Problem in Hardware Input
    By kirly in forum New To Java
    Replies: 4
    Last Post: 01-22-2010, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •