Click here to Skip to main content
15,444,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.*;
public class Customer
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter name");
String name=s.nextLine();
System.out.println("enter age");
int age=s.nextInt();
System.out.println("enter gender");
String gender=s.next();
System.out.println("Hailing from:");
String city=s.nextLine(); /* It doesn't take any input and directly display name and so on.*/

//Dispay customer details

System.out.println(name);
System.out.println(age);
System.out.println(gender);
System.out.println(city);
}
}

What I have tried:

s.nextLine(); /*After "String city=s.nextLine()" so that it consume new line otherwise city won't take space but it doesn't display any output after adding it.*/
Posted
Updated 23-Jun-19 8:53am

Replace
String gender=s.next();
With
String gender=s.nextLine();
 
Share this answer
 
Comments
Member 13954890 23-Jun-19 4:37am    
it's not taking gender after that and directly asking for Hailing from.
You need the scanner to consume the line end character(s) after getting the gender. Change the code by adding s.nextLine after getting the gender, thus:
Java
String gender=s.next();
s.nextLine(); // add this to consume the end of line.
System.out.println("Hailing from:");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900