-2

I have created a program that allows a user to keep guessing numbers until they either guess the correct number or enter end. I have used a do-while loop to do this. When I create a new scanner inside the loop body it works as expected. However if I create it outside the loop body, it works fine if the input is integers or the first input is end However if the input end follows integer inputs it doesn't pick up the nextLine() until the next loop. Is there a way to do this without having to creat a new scanner object each time.

    private static void guessingGame() {
    Scanner sc = new Scanner(System.in);        
    int answer = 7;
    String input = "";
    int number = 0;
    
    do {  
        //Scanner sc = new Scanner(System.in); 
        System.out.print("Guess a number between 1 and 10 or end to finish ");
        System.out.println("input at start is: " + input);
        boolean b = sc.hasNextInt();
        if(b) {
            number = sc.nextInt();
            System.out.println("number  is: " + number);  //for testing code
        }else {
            input = sc.nextLine();
            System.out.println("input is: " + input);   //for testing code
        }  
        if (number == answer) {
            System.out.println("Correct Guess");
            break;
        }else {
            if(input.equals("end")) System.out.println("Hope you enjoyed the game");
            else System.out.println("Incorrect Guess, try again ");
        } 
        System.out.println("input before while is: " + input);  //for testing code
    }while(number != answer && !(input.equals("end")));
    
}

Example output for when end follow an integer input:enter code here number is: 3 Incorrect Guess, try again input before while is: Guess a number between 1 and 10 or end to finish input at start is: end input is: Incorrect Guess, try again input before while is: Guess a number between 1 and 10 or end to finish input at start is: input is: end Hope you enjoyed the game input before while is: end

  • 2
    I'm not entirely sure if this will answer your question: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Lino Jan 18 '21 at 15:26
  • It is fine if anyone downvotes a questions , but please have the courtesy to explain the reason for downvoting a question. – Umeshwaran Jan 18 '21 at 15:53

1 Answers1

0

you can solve this by using a while loop .

See the following code.

private static void guessingGame() {
        Scanner sc = new Scanner(System.in);        
        int answer = 7;
        String input = "";
        int number = 0;
        
        while(!input.equals("end")) {  
            //Scanner sc = new Scanner(System.in); 
            System.out.print("Guess a number between 1 and 10 or end to finish ");
            System.out.println("input at start is: " + input);
            boolean b = sc.hasNextInt();            
            if(b) {
                number = sc.nextInt();
                System.out.println("number  is: " + number);  //for testing code
            }else {
                input = sc.next();  //Edited here . Changed nextLine() to next().
                System.out.println("input is: " + input);   //for testing code
            }  
            if (number == answer) {
                System.out.println("Correct Guess");
                break;
            }else {
                if(input.equals("end")) System.out.println("Hope you enjoyed the game");
                else System.out.println("Incorrect Guess, try again ");
            } 
            System.out.println("input before while is: " + input);  //for testing code
        }
    
    }

In here , at first , input will always be empty String . On while loop, it gets assigned to your String input i.e, end . Till it encounters end , your loop will be running.

Edited

Change input=sc.nextLine(); to input=sc.next(); . This is because , your scanner waits for next Line and doesn't consider "end" as input string .

Umeshwaran
  • 167
  • 2
  • 8
  • Thanks for the feedback but the same thing happens with the while loops – firstEagle Jan 18 '21 at 18:02
  • Incorrect Guess, try again Guess a number between 1 and 10 or end to finish input at start is: 4 Incorrect Guess, try again Guess a number between 1 and 10 or end to finish input at start is: end Incorrect Guess, try again Guess a number between 1 and 10 or end to finish input at start is: Hope you enjoyed the game – firstEagle Jan 18 '21 at 18:03
  • what is the order of your input for testing . – Umeshwaran Jan 18 '21 at 18:18
  • The iprogram as written worked for inputs it they were integers. So for example first guess 4, second gues 8 etc. It also worked if end was the only input. Where is doesn't work is if the user makes a guess then follows it with end e.g. first guess 5, next input end. I have solved it by taking the fist input outside a while loop and taking new input at the end of the while loop . However I would like to understand why the previous version did not see the nextLine input until it exited and re-entered the loop (unless the scanner object was re-created at the start of each loop) – firstEagle Jan 18 '21 at 18:51