我在Java中使用扫描仪,并且试图让程序仅在用户检测到整数供用户选择但我编写的代码未提供该功能时才继续运行。这是我的代码:

import java.util.Scanner;

/**
 *
 * @author Ansel
 */
public class Test {

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);

        AddressBook ad1 = new AddressBook();
        String firstName="";
        String lastName="";
        String key="";
        String street="";
        String city="";
        String county="";
        String postalCode="";
        String mNumber="";
        int choice=0;
      do{

        System.out.println("********************************************************************************");
        System.out.println("Welcome to the Address book. Please pick from the options below.\n");
        System.out.println("1.Add user \n2.Remove user \n3.Edit user \n4.List Contact \n5.Sort contacts \n6.Exit");

          System.out.print("Please enter a choice: ");
          int reloop = 0;
        do {
         try {
             scan.nextLine();
             choice = scan.nextInt();
              reloop ++;
        } catch (Exception e) {
       System.out.println ("Please enter a number!");
  }} while(reloop == 0);

        if(choice==1){
            //Add user
            System.out.print("Please enter firstname: ");
            firstName=scan.next();
            System.out.print("Please enter lastname: ");
            lastName=scan.next();
            scan.nextLine();
            System.out.print("Please enter street:");
            street=scan.nextLine();
            System.out.print("Please enter city: ");
            city=scan.next();
            System.out.print("Please enter county: ");
            county=scan.next();
            System.out.print("Please enter postal code: ");
            postalCode=scan.next();
            System.out.print("Please enter Mobile number: ");
            mNumber=scan.next();
            Address address = new Address(street,city,county,postalCode,mNumber);
            key = lastName + " ".concat(firstName);
            Person person = new Person(firstName,lastName,address);
            ad1.addContact(key,person);
            System.out.println("key: " + key);
        }

        else if(choice==2){
            //Remove user

            System.out.print("Please enter name of user to remove: ");
            key=scan.nextLine();
            System.out.println("name:" + key);
            ad1.removeContact(key);
        }

        else if(choice==3){
            //Edit user
        }

        else if(choice==4){
            //List contact
            System.out.println("Enter name of contact you wish to lookup: ");
            key=scan.nextLine();
            ad1.listContact(key);

        }

       else if(choice==5){
            //Sort contacts
        }
       else{
            System.out.println("Invalid choice entered, please try again");
       }

      }while(choice!=6);
    }
}


无法正常运行的主要代码是:

int reloop = 0;
        do {
         try {
             scan.nextLine();
             choice = scan.nextInt();
              reloop ++;
        } catch (Exception e) {
       System.out.println ("Please enter a number!");
  }} while(reloop == 0);


该代码在运行时要求输入数字。例如,如果您输入一个字母,它将显示一个空行,直到您输入另一个字母,然后说请输入一个数字。我不明白为什么它不说请输入字母或除int以外的其他任何数字

最佳答案

只需使用Scanner.nextLineInteger.parseInt即可避免混淆。

Scanner scan = new Scanner(System.in);
int choice = 0;
System.out.print("Please enter a choice: ");
int reloop = 0;
do {
  try {
    String input = scan.nextLine(); // Scan the next line from System.in
    choice = Integer.parseInt(input); // Try to parse it as an int
    reloop++;
  } catch (Exception e) {
    System.out.println("Please enter a number!");
  }
} while (reloop == 0);


您还可以在每个nextLine之后使用nextInt终止行,但是如上所述,我更喜欢分别解析int。它更清晰,更详细。

10-08 16:14