我在Scanner.nextInt()之后输入一个Scanner.nextLine()来使用新行,但它不能正常工作[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我目前正在使用Udemy课程学习Java编程(准确地说是Tim Bulchalka的完整Java大师班)并且正在学习ArrayList。

我正在尝试将应用程序编程为像手机一样,其中可以包含联系人号码。

这是我的主要src(或者你称它为什么?),减去一些不相关的(或遇到相同问题的代码):

package com.timbuchalka;

import java.util.Scanner;

public class Main {

private static Scanner scanner2 = new Scanner(System.in);

public static void main(String[] args) {
    MobilePhone phone = new MobilePhone();
    boolean quit = false;
    int choice = 0;
    printInstructions();
    while(!quit) {
        System.out.println("What do you want to do?");
        choice = scanner2.nextInt();
        scanner2.nextLine();

        switch (choice) {
            case 1:
                phone.printContact();
                break;
            case 2:
                phone.addContact();
                break;
            ........
            case 7:
                quit = true;
                break;
        }
    }
}

public static void printInstructions() {
    System.out.println("\nPress ");
    System.out.println("\t 0 - To see instructions again.");
    System.out.println("\t 1 - To see all contacts.");
    System.out.println("\t 2 - To add new contact.");
    ......
    System.out.println("\t 7 - To quit the application.");
}

}

这是我的Contacts类(在MobilePhone类中):

package com.timbuchalka;

import java.util.ArrayList;
import java.util.Scanner;

public class Contacts {

private static Scanner scanner = new Scanner(System.in);

private ArrayList<Integer> listPhoneNumber = new ArrayList<Integer>();
private ArrayList<String> listName = new ArrayList<String>();

public void addContact() {
    System.out.println("Please enter the contact name: ");
    String name = scanner.nextLine();
    System.out.println("Please enter the contact number: ");
    int number = scanner.nextInt();
    addContact(name, number);
}

public void printContacts() {
    System.out.println("Contact Details:");
    for (int i=0; i<listPhoneNumber.size(); i++) {
        System.out.println((i+1) + ". " + listName.get(i) + " - " + listPhoneNumber.get(i));
    }
    System.out.println("\t");
}

private void addContact(String name, int number) {
    listName.add(name);
    listPhoneNumber.add(number);
}

通过申请时,第一轮工作正常。我可以打印和/或添加联系人到我的Contacts类。但是,当我尝试添加其他联系人时,我无法输入联系人姓名。控制台直接跳过输入输入联系号码。这是一个例子:

Press 
     0 - To see instructions again.
     1 - To see all contacts.
     2 - To add new contact.
     3 - To modify a contact's name.
     4 - To modify a contact's number.
     5 - To remove a contact's details.
     6 - To search for a contact's details.
     7 - To quit the application.
What do you want to do?
1
Contact Details:

What do you want to do?
2
Please enter the contact name: //the 1st time it doesn't skip this step
John
Please enter the contact number: 
12345
What do you want to do?
2
Please enter the contact name: // however during the 2nd time, this step is skipped
Please enter the contact number: 
3
What do you want to do?
1
Contact Details:
1. John - 12345
2.  - 3

我可以知道为什么会这样吗?而且,我可以知道如何解决这个问题吗?非常感谢你!

java arraylist java.util.scanner
2个回答
1
投票

Scanner读取时,我发现最好总是使用nextLine()然后手动解析数据。

这使您可以始终如一地使用整行数据。并且您可以更仔细地解析用户输入。

所以,在你的情况下,你不是使用nextInt()读取选项,而是使用nextLine()然后使用Integer.parseInt()


0
投票

每次调用scanner.nextInt()都会使用scanner.nextLine()跟随它,以便吞下行尾令牌。

© www.soinside.com 2019 - 2024. All rights reserved.