1

I wanted to create a loop where I can input my name using a Scanner, but the system keeps spamming "Gimme your name" and doesn't leave me the chance to input my name. I want the system to output "Gimme your name" and wait for my input.

Scanner sc = new Scanner(System.in);
char reponse = 'O';
name = sc.nextLine();
while (reponse=='O')

    System.out.println("Gimme your name! ");
    name = sc.nextLine();
    System.out.println("Hello "+name+"How are you doing ? \n Wanna retry ? (O/N)" );
    reponse = sc.nextLine().charAt(0);
2
  • Your while only applies to System.out.println("Gimme your name! ");. Missing braces around your block Commented Dec 29, 2019 at 20:22
  • no braces for the loop ? Commented Dec 29, 2019 at 20:22

2 Answers 2

1

Try putting open close brackets around the while statement:

   char reponse = 'O';
    name = sc.nextLine();
    while (reponse=='O') {

        System.out.println("Gimme your name! ");
        name = sc.nextLine();
        System.out.println("Hello "+name+"How are you doing ? \n Wanna retry ? (O/N)" );
        reponse = sc.nextLine().charAt(0); ``` 

    }
Sign up to request clarification or add additional context in comments.

Comments

0

Because there is not a open and close bracket, the way the code was written may be read from the compiler like:

while (respose == 'O') System.out.println("Gimme your name! "); 

The ending semicolon would "combine" the two lines into one, per se.

Include the open and closed bracket after the while loop and at the end of the looping statements to fix.

1 Comment

Thank you for these explanations .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.