Reputation: 23
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);
Upvotes: 1
Views: 55
Reputation: 21
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.
Upvotes: 0
Reputation: 229
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); ```
}
Upvotes: 1