Next() and nextLine()


#1

hey everyone.

i have written a code which gets data from the user.

the code works fine when i use this code :

import java.util.Scanner;
class UserInput
{
public static void main(String [] args)
{
Scanner variable = new Scanner(System.in);

System.out.println("Enter your name : ");
String s = variable.nextLine();

System.out.println("Enter age : ");
int i = variable.nextInt();

System.out.println("Enter Decimal value : ");
float f = (float)variable.nextFloat();

System.out.println(“Enter Double value :”);
double d = variable.nextDouble();

System.out.println("Enter your Gender (m/f): ");
char c = variable.next().charAt(0);

System.out.println("Your Details are given below : ");
System.out.println("Name : ");
System.out.println(s);
System.out.println("Age : ");
System.out.println(i);
System.out.println("Gender : ");
System.out.println( c );
System.out.println("Float value : ");
System.out.println(f);
System.out.println("Double value : ");
System.out.println(d);

}
}

it works fine…

but if i change the section of nextLine() (where i ask user to enter a name ) to somewhere down in the code, the output just skips that step. The code which gives problem is down below :

import java.util.Scanner;
class UserInput
{
public static void main(String [] args)
{
Scanner variable = new Scanner(System.in);

System.out.println("Enter age : ");
int i = variable.nextInt();

System.out.println("Enter Decimal value : ");
float f = (float)variable.nextFloat();

System.out.println(“Enter Double value :”);
double d = variable.nextDouble();

System.out.println("Enter your name : ");
String s = variable.nextLine();

System.out.println("Enter your Gender (m/f): ");
char c = variable.next().charAt(0);

System.out.println("Your Details are given below : ");
System.out.println("Name : ");
System.out.println(s);
System.out.println("Age : ");
System.out.println(i);
System.out.println("Gender : ");
System.out.println( c );
System.out.println("Float value : ");
System.out.println(f);
System.out.println("Double value : ");
System.out.println(d);

}
}

But if I use next() function instead of nextLine() in 2nd Code, again it works fine.

I am really confused. Please help.


#2

its explained here:

https://stackoverflow.com/questions/5032356/using-scanner-nextline

nextDouble seems to include a new line character, which is then read by nextLine, preventing you to input


#3

Thanks a lot for your answer. :slight_smile:

it’s really helpful.

Although i am still in doubt. :sweat_smile:

Does it mean whenever I have to take multiple inputs(diff. datatypes), i must use nextLine() first or else other functions like nextInt, or nextDouble will generate this type of problem. (i.e. sequence is important)

and also it has no effect on next(). can you explain please?


#4

we are programming, we can surely make it work in the order we like

then you need to understand the difference between next and nextLine, which is explained here:

https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class

seems a different separator is used for the different methods.


#5

Thanks a lot again…:smiley:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.