Features of using scanner. nextLine()

I have this code.

String anun=sc.nextLine();
System.out.println("age:");
int tar=sc.nextInt();
System.out.println("email:");
String mail=sc.nextLine();
System.out.println(mail);
System.out.println("email:" + mail);

And always, when the time comes for String mail=sc.nextLine();, the step is simply skipped. What is the problem?

Author: Pavel Parshin, 2016-03-03

1 answers

In this line, you counted only the number (not considering everything that can go further, including the end of the line):

sc.nextInt();

Next, when calling

sc.nextLine();

The end of the line is read (starting from the previously entered number and ending with the line feed). You can add an additional call sc.nextLine(); after each call nextInt();.

 13
Author: Pavel Parshin, 2016-03-03 18:43:48