繁体   English   中英

扫描器Java验证和多个实例

[英]scanner java validation and multiple instances

我是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();

因此,我有两个create 3扫描仪实例,并且还使用一个字符串在while循环中将错误的输入分配给能够再次提示。 有什么建议吗? 我相信有更好的方法,但我会尽力理解。

谢谢!

我不确定我是否理解您遇到的问题,但是扫描仪有一些奇怪的行为,这些行为不会立即显现出来。 例如,如果键入“ 1234bubble”,然后按Enter,则nextInt()将返回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();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM