2
    System.out.print("Name : ");
    String name = in.nextLine();

    System.out.print("Age : ");
    int age = in.nextInt();

    System.out.print("City : ");
    String city = in.nextLine();

the output will be :

Name : test

Age : 20

BUILD SUCCESSFUL

when i debug them, it won't read the user input for "city" . but when i changed the data type for "age" to string, it will read. how can i kept the data type of age to int with the system reading the user inputs for city ?

Athirah Hazira
  • 473
  • 2
  • 15
  • 37
  • @athirahhazira94 , you have go through on about nextLine(). Anda kena baca tentang nextLine(). – gjman2 Oct 21 '13 at 03:02

3 Answers3

8

As there is still a new line character in the buffer after reading the age, change it to..

System.out.print("Name : ");
String name = in.nextLine();

System.out.print("Age : ");
int age = in.nextInt();
//add
in.nextLine();

System.out.print("City : ");
String city = in.nextLine();
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

There is still a new line character in the buffer after reading the age. Try adding a in.nextLine before you ask for the city value.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Try this,

System.out.print("Age : ");
int age = Integer.parseInt(in.nextLine());
newuser
  • 8,338
  • 2
  • 25
  • 33