Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scanner java验证和多个实例_Java - Fatal编程技术网

scanner java验证和多个实例

scanner java验证和多个实例,java,Java,我是java新手,正在做作业。 我必须向用户请求3个输入,并进行验证。 如果我只用一个扫描器的实例,我会搞得一团糟 如果我使用三个实例进行一些变通,我的代码就可以工作。 只是我想这不是最好的做法。 我已经读了一些关于扫描仪的手册,但无法理解这个问题 谢谢 enter code here Scanner input=new Scanner(System.in); Scanner input2=new Scanner(System.in); int

我是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();
因此,我有两个create3scanner实例,并使用一个字符串将while循环中的错误输入分配给能够再次提示的。 有什么建议吗? 我相信有更好的办法,但我会尽力去理解


谢谢

我不确定我是否理解您遇到的问题,但扫描仪有一些奇怪的行为,这些行为并不是很明显。例如,如果键入“1234bubble”,然后按enter键,则nextLine()将返回1234,下一条nextLine()将显示“bubble”。对于这样的输入,这通常不是期望的行为,因为“1234bubble”不是整数,当用户按下enter键时,应该失败

因此,我通常只使用函数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();
    }

}

我不确定我是否理解你们的问题所在,但扫描器有一些奇怪的行为并不是很明显。例如,如果键入“1234bubble”,然后按enter键,则nextLine()将返回1234,下一条nextLine()将显示“bubble”。对于这样的输入,这通常不是期望的行为,因为“1234bubble”不是整数,当用户按下enter键时,应该失败

因此,我通常只使用函数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();
    }

}

这可能会把事情弄清楚。这可能会把事情弄清楚。谢谢丹尼尔,我会检查你的代码。问题是我必须打印3个提示。。。一个问整数,一个问双精度,一个问字符串。试过了,效果很好。。。会更好地研究它以了解我做错了什么。。但是是的,也许答案只是获取完整字符串并在外部解析它。。非常感谢汉克斯·丹尼尔,我会检查你的密码。问题是我必须打印3个提示。。。一个问整数,一个问双精度,一个问字符串。试过了,效果很好。。。会更好地研究它以了解我做错了什么。。但是是的,也许答案只是获取完整字符串并在外部解析它。。谢谢