Reputation: 3
I am facing problem in the following code please have a look on it and help me solve my problem. The Code is :
import java.util.*;
public class Test1{
public static void main(String ...args){
Scanner s1=new Scanner(System.in);
int age;
age= s1.nextInt();
System.out.println(age);
System.out.println("Enter Name");
String name;
s1.next();
name = s1.nextLine();
System.out.println(name);
}
}
Input of the Program:
12
12
Enter Name
A B C
B C
Expected Output:
12
12
Enter Name
A B C
A B C
I was trying the Scanner class.Please Help me to Get my Expected Output, and specify my mistake.Please explain you answer Thank you so much.
Upvotes: 0
Views: 278
Reputation: 66
import java.util.*;
public class Test1
{
public static void main(String ...args){
Scanner s1=new Scanner(System.in);
int age;
age= s1.nextInt();
System.out.println(age);
System.out.println("Enter Name");
s1.nextLine();
String name = s1.nextLine();
System.out.println(name);
}
}
This should work.
sc.next() uses <space> as a delimiter by default.
Upvotes: 1