0

Here is a basic while loop program, where the program at the end should ask the user if you want to keep going or not. The bug is that the program doesn't let me input (y/n) which is the last String Input.

This does not happen when the last input is an integer value.

import java.util.Scanner;

public class lol {
    
    public static void main(String[] args) {
        
    Scanner sc = new Scanner(System.in);

    int age;
    String name = "";
    String height = "";
    String userOption = "";

        while (!userOption.equals("n"))
        {
            
            System.out.println();
            System.out.print("Enter your name: ");
            name = sc.nextLine();
 
            System.out.println();

            System.out.print("Enter your age: ");
            age = sc.nextInt();

            System.out.println();

            System.out.print("Enter your height: ");
            height = sc.nextLine();

            System.out.println();
            
            System.out.println("Do you want to keep going? (y/n)");
               
            // The program over looks this line of code
            userOption = sc.nextLine();
            
            if(userOption.equals("y"))
            {
                System.out.println("Breaking");
                break;
            }
            else
            {
                continue;
            }
            
        }
    
    }



        
}
  • Well an Integer value does not equal "y", and you tell it to continue the loop in that case. – daniu Apr 10 '22 at 09:36
  • Does this 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) – Progman Apr 10 '22 at 11:12

2 Answers2

1

See this topic, additional nextLine() call could be a workaround

Scanner is skipping nextLine() after using next() or nextFoo()?

Andrei Prakhov
  • 206
  • 1
  • 5
0

Minor adjustments to the code will fix its behaviour ;)

    Scanner sc = new Scanner(System.in);

    int age;
    String name = "";
    String height = "";
    String userOption = "";

    while (true) {

        System.out.print("Enter your name: ");
        name = sc.nextLine();

        System.out.println();

        System.out.print("Enter your age: ");
        age = sc.nextInt();

        System.out.println();

        System.out.print("Enter your height: ");
        height = sc.nextLine();

        System.out.println();

        System.out.println("Do you want to keep going? (y/n)");

        // The program over looks this line of code
        userOption = sc.nextLine();

        if (userOption.equals("y")) {
            // nothing
            System.out.println("Continuing");
        } else {
            System.out.println("Stopping");
            break;
        }

    }

    System.exit(0);

I would however agree that you should take a look at Scanner is skipping nextLine() after using next() or nextFoo()?

GJohannes
  • 1,408
  • 1
  • 8
  • 14