如何在使用扫描仪进行输入收集时验证用户输入

时间:2013-11-22 13:27:26

标签: java

描述

我的目标是请求用户仅为触发异常的条目提供输入,排除那些令人满意的输入请求

实施例

注意inputScanner的实例

public Fluid addFluid() {
    do{
        try{
            System.out.println("Please enter fluids ID: "); 
            f.setFluidID(input.next());
            System.out.println("Please enter fluids molecular weight: ");   
            f.setMolecularWeight(input.nextDouble()); 
            System.out.println("Please enter fluids temperature: ");            
            f.setTemperature(input.nextDouble());
            error = false;
        }
        catch(InputMismatchException e){
            System.out.println("Error! Please provide the right input.");
           // if exception happens on input for setMolecularWeight(input.nextDouble()); skip print statement for fluids ID and f.setFluidID(input.next());
        }
    }
    while(error != false);
    getFluid().add(f);
    System.out.println(getFluid());
    return f;
}

输出

Please enter fluids ID: 
propane
Please enter fluids molecular weight: 
a
Error! Please provide the right input.
Please enter fluids ID:                // how to exclude this print statement
Please enter fluids molecular weight: 

是否有办法使用if / else结构执行此操作。我想避免为每个输入操作符执行try / catch。 非常感谢提前。

4 个答案:

答案 0 :(得分:3)

简答:

调用nextDouble()后,您需要清除扫描程序上的缓冲区,因为InputStream将包含换行符\n

input.next();input.nextLine();添加到catch块的末尾应该可以跳过额外的\n

(请参阅java.util.Scanner : why my nextDouble() does not prompt?

说明:

  • 即使nextDouble阻止执行直到用户输入换行符\n,它也只返回Scanner缓冲区中有效字符的字符(将正确解析为{的字符) {1}})。这意味着
  • 如果输入的输入不解析为double,则这些无效字符仍然位于缓冲区中。当您再次尝试读取double时,它将在现有输入上重复相同的过程,从而产生无限循环。
  • 因此,您需要在继续之前“清除”缓冲区。由于似乎没有这种方法,您只需要将扫描仪推进到缓冲区的末尾。在这种情况下,nextDouble就足够了。

(对于我jonhopkins无耻地抓取的代码,向用户his answer致信)

input.next();

注意:在阅读第2和第3个输入之前,您甚至可能需要额外的System.out.println("Please enter fluids ID: "); while (true) { try { f.setFluidID(input.next()); break; // break out of the current loop } catch (InputMismatchException e) { System.out.println("Error! Please provide the right input."); input.next(); } } System.out.println("Please enter fluids molecular weight: "); while (true) { try { f.setMolecularWeight(input.nextDouble()); break; // break out of the current loop } catch (InputMismatchException e) { System.out.println("Error! Please provide the right input."); input.next(); } } System.out.println("Please enter fluids temperature: "); while (true) { try { f.setTemperature(input.nextDouble()); break; // break out of the current loop } catch (InputMismatchException e) { System.out.println("Error! Please provide the right input."); input.next(); } } 调用(我没有对此进行测试)。

答案 1 :(得分:2)

只需将它放在循环之外:

public Fluid addFluid() {
    System.out.println("Please enter fluids ID: "); 
    f.setFluidID(input.next());

    do{
        try{    
            System.out.println("Please enter fluids molecular weight: ");   
            f.setMolecularWeight(input.nextDouble()); 
            System.out.println("Please enter fluids temperature: ");            
            f.setTemperature(input.nextDouble());
            error = false;
        }
        catch(InputMismatchException e){
            System.out.println("Error! Please provide the right input.");
           // if exception happens on input for setMolecularWeight(input.nextDouble()); skip print statement for fluids ID and f.setFluidID(input.next());
        }
    }
    while(error != false);
    getFluid().add(f);
    System.out.println(getFluid());
    return f;
}

现在只打印一次,而其他输入可以重复。 如果您想检查分子量和流体温度的个别问题,您将不得不重复尝试捕获。

答案 2 :(得分:1)

不漂亮,但应该有效:

public Fluid addFluid() {
    boolean alreadyAnswered1 = false;
    boolean alreadyAnswered2 = false;
    boolean alreadyAnswered3 = false;
    do{
        try{
            if (!alreadyAnswered1) {
                System.out.println("Please enter fluids ID: ");
                 f.setFluidID(input.next());
                 alreadyAnswered1 = true;
            }
            if (!alreadyAnswered2) {
                System.out.println("Please enter fluids molecular weight: ");
                f.setMolecularWeight(input.nextDouble());
                alreadyAnswered2 = true;
            }
            if (!alreadyAnswered3) {
                System.out.println("Please enter fluids temperature: ");
                f.setTemperature(input.nextDouble());
                alreadyAnswered3 = true;
            }
            error = false;
        }
        catch(InputMismatchException e){
            System.out.println("Error! Please provide the right input.");
           // if exception happens on input for setMolecularWeight(input.nextDouble()); skip print statement for fluids ID and f.setFluidID(input.next());
        }
    }
    while(error != false);
    getFluid().add(f);
    System.out.println(getFluid());
    return f;
}

答案 3 :(得分:1)

在我看来,您想要为抛出异常的任何一个重复输入请求。我只想使用一些较小的循环,每个循环都有自己独立的try-catch块。

System.out.println("Please enter fluids ID: ");
while (true) {
    try {
        f.setFluidID(input.next());
        break; // break out of the current loop
    } catch (InputMismatchException e) {
        System.out.println("Error! Please provide the right input.");
    }
}

System.out.println("Please enter fluids molecular weight: ");
while (true) {
    try {
        f.setMolecularWeight(input.nextDouble());
        break; // break out of the current loop
    } catch (InputMismatchException e) {
        System.out.println("Error! Please provide the right input.");
    }
}

System.out.println("Please enter fluids temperature: ");
while (true) {
    try {
        f.setTemperature(input.nextDouble());
        break; // break out of the current loop
    } catch (InputMismatchException e) {
        System.out.println("Error! Please provide the right input.");
    }
}

这将导致程序为每个输入循环,直到该输入没有抛出异常。它还消除了一直持续到error == false的外循环的需要。如果您希望在错误消息之后再次显示输入提示,只需将提示放在各自的while循环中。