Put it like this:
int age = input.nextInt();
input.nextLine();
System.out.println("Great! We're almost done. What are three interests you have?");
String interests = input.nextLine();
The rest of the code could be equals to the code that you have above.
EDIT: It has to be like this (I mean, without storing the line in any of your variables) because the nextInt()
function don't read the full line, just the next integer. So, when the nextInt()
function read the int
, the "cursor" of the Scanner
will be in the position after the int
.
For example, if you put more than one words (separated by a space) when you are trying to reading the int
, in your interests variable you will read the rest of the line that the nextInt()
couldn't read before. So, if you have:
System.out.println("Okay, " + name + ", you are a " + gender + ". Now, tell me, what is your age?");
//Here you enter --> 18 This is a prove
int age = input.nextInt();
System.out.println("Great! We're almost done. What are three interests you have?");
//Here you won't be able to put anything
String interests = input.nextLine();
Now you will have stored:
age = 18;
interests = "This is a prove";
But if you put the code like this:
System.out.println("Okay, " + name + ", you are a " + gender + ". Now, tell me, what is your age?");
//Here you enter --> 18 This is a prove
int age = input.nextInt();
input.nextLine();
//Now the Scanner go to a new line (because of the nextLine) but without storing the value
System.out.println("Great! We're almost done. What are three interests you have?");
//Now you are in a new line and you are going to be able to write. For example --> I am reading this.
String interests = input.nextLine();
The values that you are going to have now it's:
age = 18;
interests = "I am reading this.";
So, in conclusion, if you have a nextInt()
and you are trying to read a single int
you will read it, but the cursor will stay at the final of this line and you won't be able to read the next line. For this, you have to read the full line without storing it with input.nextLine();
.
I expect it will be helpful for you!