-1

So everything compiles fine but when I run it the line asking for city and the line asking for zip both print out at the same time. I need them to print individually so the user can answer.

import java.util.Scanner;
public class PersonalInfo
{
   public static void main(String[] args)
   {
     String name, city, state, major;
     int zip, phone, address;

     Scanner scanner = new Scanner(System.in);

     System.out.println("Please enter your name: ");
     name = scanner.nextLine();

     System.out.println("please enter your address number: ");
     address = scanner.nextInt();

     System.out.println("Please enter the name of the city you live in: ");
     city = scanner.nextLine();

     System.out.println("Please enter your zip code: ");
     zip = scanner.nextInt();

     System.out.println("Please enter the name of the state you live in: ");
     state = scanner.nextLine();

     System.out.println("Please enter your phone number(format ##########): ");
     phone = scanner.nextInt();

     System.out.println("Please enter your college major: ");
     major = scanner.nextLine();

     System.out.println(name + "\n" + address + "," + city + "," + state +
                        "," + zip + "\n" + phone + "\n" + major);
   }
}

1 Answers1

0

The only method that consume newline of the input is nextLine(), so if you use nextInt() and then you want to capture anything else you have to call a nextLine() after you call nextInt().

For example:

System.out.println("please enter your address number: ");
address = scanner.nextInt();

scanner.nextLine();

System.out.println("Please enter the name of the city you live in: ");
city = scanner.nextLine();

Output:

please enter your address number: 
567
Please enter the name of the city you live in: 
Puerto Montt
Rcordoval
  • 1,792
  • 1
  • 15
  • 24