I am doing the daily challenges from /r/dailyprogrammer and am working on the ve
ID: 3779120 • Letter: I
Question
I am doing the daily challenges from /r/dailyprogrammer and am working on the very first one. I am trouble with the final output printing before taking in the input of rUserName. How would I delay this until it had the input?
import java.util.Scanner;
public class Easy1 {
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
String name;
int age;
String rUserName;
// Ask for users name
System.out.println("Enter your name.");
name = keyboard.nextLine();
// Ask for users age
System.out.println("Enter your age.");
age = keyboard.nextInt();
// Ask for reddit user name
System.out.println("Enter your reddit user name.");
rUserName = keyboard.nextLine();
// Tell user their name, age, and username.
System.out.println("Your name is " + name + ", your age is " + age + ", and your reddit username is " + rUserName);
}
}
Explanation / Answer
add just keyboard.nextLine() after age=keyboard.nextInt(); It will then work...
The problem is with the keyboard.nextInt() command it only reads the int value. So when you continue reading with input.nextLine() you receive the " " i.e new line and Enter key. So to skip this you have to add the keyboard.nextLine()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.