扫描程序java验证和多个实例

时间:2015-11-20 21:04:55

标签: java

我是java的新手并且正在做作业。 我必须要求用户输入3个输入并进行验证。 如果我只使用一个扫描仪实例,那么我就搞砸了。

如果我使用三个实例和一些解决方法,我的代码可以工作。 只有我猜这不是最好的做法。 我一直在阅读有关扫描仪的手册,但无法理解问题

由于

enter code here
Scanner input=new Scanner(System.in);               
Scanner input2=new Scanner(System.in);          

int input_integer=0;
double input_double=0.0;
String input_string="";
double value=0;

System.out.print("\n Please enter a number: ");     

    while(!input.hasNextInt()){ 
        System.out.println("***** Error: the char inserted is not a number! *****");
        String input_wrong=input.next();
        System.out.print("\n Please enter a number: ");     
    }   

    input_integer=input.nextInt();

    System.out.print("\n Please enter a double: ");     
    while(!input.hasNextDouble()){  
        System.out.println("***** Error: the char inserted is not a double! *****");
        String input_wrong=input.next();
        System.out.print("\n Please enter an double: ");    
    }           
    input_double=input.nextDouble();

    System.out.print("\nPlease enter a string: ");          
    input_string=input.nextLine();

所以我有两个创建3个扫描程序实例,并且还使用字符串在while循环中将错误的输入分配给能够再次提示。 有什么建议吗? 我相信有更好的方法,但我会尝试理解..

谢谢!

1 个答案:

答案 0 :(得分:0)

我不完全确定我理解你遇到了什么问题,但扫描仪有一些奇怪的行为,这些行为并不是很明显。例如,如果您键入" 1234bubble"然后按回车键,然后nextInt()将返回1234,下一个nextLine()将说" bubble"。对于像这样的输入,这通常不是所希望的行为,因为" 1234气泡"不是整数,当用户按下回车时应该失败。

因此,我通常只使用函数nextLine()。然后,我只使用Integer.parseInt(..)等函数手动处理数据。这样,我可以保证我能以清晰明了的方式处理整条生产线,这与其他造成混乱代码的技术不同。

以下是我编写程序的方法:

import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class Main
{
    static Random rand = new Random();

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

        int input_integer = 0;
        double input_double = 0.0;
        String input_string = "";
        double value = 0;

        while (true)
        {
            System.out.print("Please enter an integer: ");

            // Get the entire next line of text
            String text = input.nextLine();

            try
            {
                // Try to turn the line into an integer
                input_integer = Integer.parseInt(text);

                // Turning it into an int succeeded!
                // Leave the while loop
                break;
            } catch (NumberFormatException e)
            {
                // Turning it into an int failed.
                System.out.println("***** Error: the text inserted is not an integer! *****");  
            }
        }

        while (true)
        {
            System.out.print("Please enter a double: ");

            // Get the entire next line of text
            String text = input.nextLine();

            try
            {
                // Try to turn the line into a double
                input_double = Double.parseDouble(text);

                // Turning it into an double succeeded!
                // Leave the while loop
                break;
            } catch (NumberFormatException e)
            {
                // Turning it into an double failed.
                System.out.println("***** Error: the text inserted is not a double! *****");    
            }
        }

        System.out.print("Please enter a string: ");
        input_string = input.nextLine();

        // This is done automatically when the program stops, but it's
        // a good habit to get into for longer running programs.
        input.close();
    }

}