0

So I am a new Java programmer and I am trying to figure out why a piece of code isn't working. The issue I am having is with the line: "String interests = input.nextLine();", it skips the user's input and jumps to the next System.out, so it just displays "Your profile..." in the console before allowing for the user to input any data. Sorry if it's a dumb question, I'm very new!

System.out.println("Hello, " + name + "! What is your gender?");
String gender = input.nextLine();
System.out.println("Okay, " + name + ", you are a " + gender + ". Now, tell me, what is your age?");
int age = input.nextInt();
System.out.println("Great! We're almost done. What are three interests you have?");
String interests = input.nextLine();

System.out.println("...Your profile...");
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
Francisco Romero
  • 11,900
  • 18
  • 71
  • 151
Dan Kunj
  • 13
  • 3

5 Answers5

0

try instead System.console().readLine();

Noize
  • 147
  • 3
  • 16
  • Thanks for the suggestion! I tried this and now I'm getting this error: "Exception in thread "main" java.lang.NullPointerException at DatingSimGame.main(DatingSimGame.java:11)" – Dan Kunj Jun 18 '15 at 23:25
0

Please show how you declare your Scanner.

Just for reference, it is done this way.

Scanner sc = new Scanner(System.in);
...
System.out.println("Hello, " + name + "! What is your gender?");
String gender = input.nextLine();
...
Rey Libutan
  • 4,907
  • 7
  • 34
  • 67
0

I am assuming When you Declared Scanner you didn't do Scanner input = new Scanner(System.in) you need to put System.in in the parentheses when you use scanner. so it is String gender = input.nextLine();

bokhyfg
  • 11
  • 4
  • Scanner input = new Scanner(System.in); I did, that's sort of where the confusion is. It works in other areas, but not in that one spot. – Dan Kunj Jun 18 '15 at 23:32
  • it needs to be `String age = input.nextLine();` `int x =Integer.parseInt(age);` `System.out.println("Age: "+x); – bokhyfg Jun 18 '15 at 23:56
  • You need to `parse int` to put it into a string. – bokhyfg Jun 18 '15 at 23:57
0

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!

Francisco Romero
  • 11,900
  • 18
  • 71
  • 151
  • 1
    Thank you so much. Your response was incredibly helpful and I have learned a lot from this. It made my program run flawlessly! Thanks again! -Dan – Dan Kunj Jun 19 '15 at 01:01
0

Your:

String interests = input.nextLine();

Is being skipped because there is not end of a line, look here for more info and you could try reemplace all yours nextLine() with next() only.

Community
  • 1
  • 1
Alex Sifuentes
  • 2,553
  • 2
  • 19
  • 33