为什么Scanner.next()不接受键盘输入?

时间:2016-02-13 00:09:19

标签: java netbeans

import java.util.InputMismatchException;
import java.util.Scanner;

class ClassB {

    public static void main(String[]args) throws Exception{
        ClassB b = new ClassB();
        b.getInput();
    }

    public void getInput() throws Exception {
        String label = null;
        int input;

        Scanner scan = new Scanner(System.in);
        for (input = 1; input != 4; input++){
            switch(input){
            case 1: label = "name";
            break;
            case 2: label = "password";
            break;
            case 3: label = "Room number";
            break;
            }
            System.out.println("Enter your " + label);
            scan.next();
            try{
                if (input == 1){
                    int name = scan.nextInt();
                    //residence.changeName(name);       
                }
                else if(input == 2){
                    String password = scan.next();
                }
                int rmNumber = scan.nextInt();
            }catch (IllegalArgumentException | InputMismatchException  me ){
                String type = "A string";
                String message = (input == 1 || input == 2) ? type : "An integer";
                input = 1;
            }
        }
    }
}

当我运行此代码时,try-catch块中的第一个scan.next()在按下计算机的回车键时不响应,因此,无法输入后续的if()语句。文本字段中的光标仅断行,但从不接受输入。 我正在使用netbean IDE

2 个答案:

答案 0 :(得分:1)

http://javatutorialhq.com/java/util/scanner-class-tutorial/

  

nextInt方法不会读取输入的最后一个换行符,因此在下次调用nextLine时会使用该换行符。要解决此问题,只需使用next而不是nextline,但如果您坚持使用nextLine,请在nextInt之后添加另一个scan.nextLine()。看看下面的片段

- >在scan.nextInt()

之后添加scan.nextLine()

答案 1 :(得分:0)

从您的回复中您仍然看到它停滞不前并要求用户多次输入值,我不知道代码差异是什么。这是我测试过的代码,但没有遇到这个问题(无论如何都适合我)。

import java.util.Scanner;

public class TestInput{
    private void display(String s){
        System.out.println("Please input a valid value: " + s);
    }

    public void getInput(Residence residence){
        String label = null;
        int input;
        Scanner scan = new Scanner(System.in);

        for (input = 1; input != 4; input++){

            switch(input){
                case 1: label = "name";
                break;
                case 2: label = "password";
                break;
                case 3: label = "Room number";
               break;
            }

            System.out.println("Enter your " + label);

            try{
                if (input == 1){
                    residence.changeName(scan.next());       
                }
                else if(input == 2){
                   String password = scan.next();
                   residence.changePassword(password);
                }
                else if(input == 3){
                   int roomNumber = scan.nextInt();
                   residence.changeRoomNumber(roomNumber);
                }
            }
            catch (Exception me ){
                String type = "A string";
                String message = (input == 1 || input == 2) ? type : "An integer";
                display(message);
                input = 1;
            }
        }
   }

   public static void main(String... args){
    TestInput ti = new TestInput();
    ti.getInput(new Residence());
   }
}

class Residence{
    String password;
    String name;
    int room;

    public void changeName(String n){
        this.name = n;
    }
    public void changePassword(String pass){
        this.password = pass;
    }
    public void changeRoomNumber(int n){
        room = n;
    }
}

在你的try / catch if / else语句中,你没有检查条件3。 你被“卡住”的原因是你还有第二次调用nextInt(),导致它在第一次输入房间号码后等待额外的输入。将if条件替换为下面的条件并再次尝试:

           if (input == 1){
                residence.changeName(scan.next());       
            }
            else if(input == 2){
                String password = scan.next();
                residence.changePassword(password);
            }
            else if(input == 3){
                int roomNumber = scan.nextInt();
                residence.changeRoomNumber(roomNumber);
            }