The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Help with my simple Scanner program?

Hello all,
I am new to the forum and am seeking some help on why my program is not working as expected . For some reason, my program skips over letting the user enter their town. I would be very grateful for any help that one may provide me with:

importjava.util.Scanner;publicclass Okay
{publicstaticvoid main(String[] args){String name, town;int age, siblings;
Scanner details =new Scanner (System.in);System.out.println("Hey, what's your name?");
name = details.nextLine();System.out.println("That's cool my friend is named "+ name +" too!");System.out.println("How old are you?");
age = details.nextInt();System.out.println("Wow, my friend, "+ name +", is "+ age +" years old"+" too!");System.out.println("What town do you live in?");
town = details.nextLine();System.out.println("This is getting kind of crazy... My friend, "+ name +" lives in "+ town +", too!");System.out.println("How many siblings do you have?");
siblings = details.nextInt();System.out.println("STOP RIGHT THERE. THIS IS INSANE. My friend, "+ name +", has "+ siblings +" siblings, too!!!");System.out.println("Wait, "+ name +", is that YOU!?");}}

Re: Help with my simple Scanner program?

That is because of the nextInt() method that you have used, you have read the number,
but you have not skipped the line of the user's input,
to avoid the bugs insert this code after you used nextInt() method.
details.nextLine(); that will help your program to skip the line.
hope that will help