Java 如何管理不正确的输入

Java 如何管理不正确的输入,java,Java,我试图以两个不同的整数的形式进行输入,这些整数由一个空格ex:14 2分隔 如果输入是double而不是int、字符或基本上不是由空格分隔的两个整数,我很难找到提示用户重试的方法 我真的不想使用异常和try/catch语句,因为我不理解它们 我宁愿接受字符串[]arr并使用split(“”),或者只执行console.nextInt()。任何反馈都很好。我知道它不漂亮 Scanner console = new Scanner(System.in); boolean ok = true; boo

我试图以两个不同的整数的形式进行输入,这些整数由一个空格ex:
14 2
分隔

如果输入是double而不是int、字符或基本上不是由空格分隔的两个整数,我很难找到提示用户重试的方法

我真的不想使用
异常和try/catch
语句,因为我不理解它们

我宁愿接受字符串[]arr并使用split(“”),或者只执行
console.nextInt()。任何反馈都很好。我知道它不漂亮

Scanner console = new Scanner(System.in);
boolean ok = true;
boolean ok2 = true;
while (ok = true){
for(int i = 0; i<=2; i++){

if(!console.hasNextInt()){
    kb.next();
    System.out.println("Bad input, try again");
    ok2 false;
}
else if(i = 0 && ok2 = true){
    int a = console.hasNextInt();
}
else if(i = 1 && ok2 = true){
    int b = console.hasNextInt();
    ok = false; //means there are two #s and the program can continue with a and b   

}

else{
}
 } 
扫描仪控制台=新扫描仪(System.in);
布尔ok=true;
布尔值ok2=真;
while(ok=true){

对于(int i=0;i以下代码接受输入,直到它只得到正数为止。负数和零数由while循环条件处理。字符在另一while循环条件下使用
!sc.hasnetint()
处理

       Scanner sc = new Scanner(System.in);
        int number;
        do {
            System.out.println("Please enter a positive number : ");
            while (!sc.hasNextInt()) {
                System.out.println("That is not a valid number.");
                sc.next(); 
            }
            number = sc.nextInt();
        } while (number <= 0);
        System.out.println("Recieved a Positive number = " + number+". Thanks!");

下面的代码接受输入,直到只得到正数为止。负数和零数由while循环条件处理。字符在另一个while循环条件中使用
!sc.hasnetint()
处理

       Scanner sc = new Scanner(System.in);
        int number;
        do {
            System.out.println("Please enter a positive number : ");
            while (!sc.hasNextInt()) {
                System.out.println("That is not a valid number.");
                sc.next(); 
            }
            number = sc.nextInt();
        } while (number <= 0);
        System.out.println("Recieved a Positive number = " + number+". Thanks!");

下面的代码接受输入,直到只得到正数为止。负数和零数由while循环条件处理。字符在另一个while循环条件中使用
!sc.hasnetint()
处理

       Scanner sc = new Scanner(System.in);
        int number;
        do {
            System.out.println("Please enter a positive number : ");
            while (!sc.hasNextInt()) {
                System.out.println("That is not a valid number.");
                sc.next(); 
            }
            number = sc.nextInt();
        } while (number <= 0);
        System.out.println("Recieved a Positive number = " + number+". Thanks!");

下面的代码接受输入,直到只得到正数为止。负数和零数由while循环条件处理。字符在另一个while循环条件中使用
!sc.hasnetint()
处理

       Scanner sc = new Scanner(System.in);
        int number;
        do {
            System.out.println("Please enter a positive number : ");
            while (!sc.hasNextInt()) {
                System.out.println("That is not a valid number.");
                sc.next(); 
            }
            number = sc.nextInt();
        } while (number <= 0);
        System.out.println("Recieved a Positive number = " + number+". Thanks!");

我试图写尽可能简单的代码

Scanner console = new Scanner(System.in);
System.out.print("Enter two numbers  ");

String str = console.readLine();

String values[] = str.split(" ");

int n1, n2;

try{
    n1 = Integer.parseInt(values[0]);
    //Convert first String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("First number is not integer");
}

try{
    n2 = Integer.parseInt(values[1]);
    //Convert second String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("Second number is not integer");
}
注意


此代码基于这样的假设,即用户只需输入两个元素。不超过两个。在这种情况下,需要更改代码。

我正在尝试编写尽可能简单的代码

Scanner console = new Scanner(System.in);
System.out.print("Enter two numbers  ");

String str = console.readLine();

String values[] = str.split(" ");

int n1, n2;

try{
    n1 = Integer.parseInt(values[0]);
    //Convert first String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("First number is not integer");
}

try{
    n2 = Integer.parseInt(values[1]);
    //Convert second String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("Second number is not integer");
}
注意


此代码基于这样的假设,即用户只需输入两个元素。不超过两个。在这种情况下,需要更改代码。

我正在尝试编写尽可能简单的代码

Scanner console = new Scanner(System.in);
System.out.print("Enter two numbers  ");

String str = console.readLine();

String values[] = str.split(" ");

int n1, n2;

try{
    n1 = Integer.parseInt(values[0]);
    //Convert first String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("First number is not integer");
}

try{
    n2 = Integer.parseInt(values[1]);
    //Convert second String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("Second number is not integer");
}
注意


此代码基于这样的假设,即用户只需输入两个元素。不超过两个。在这种情况下,需要更改代码。

我正在尝试编写尽可能简单的代码

Scanner console = new Scanner(System.in);
System.out.print("Enter two numbers  ");

String str = console.readLine();

String values[] = str.split(" ");

int n1, n2;

try{
    n1 = Integer.parseInt(values[0]);
    //Convert first String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("First number is not integer");
}

try{
    n2 = Integer.parseInt(values[1]);
    //Convert second String value to int, if it will be anything else than int
    // then it will throw the NumberFormatException
}catch(NumberFormatException e){
    System.out.println("Second number is not integer");
}
注意


此代码基于这样的假设,即用户只输入两个元素。不超过两个。在这种情况下,需要更改代码。

另一种解决方案使用Scanner#hasNextInt()验证输入:

    final Scanner console = new Scanner(System.in);
    final List<Integer> input = new ArrayList<Integer>();

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

        if (console.hasNextInt())
        {
            input.add(console.nextInt());
        }
        else
        {
            System.out.println("Bad input, try again ...");
            console.next();
        }
        if (input.size() >= 2)
        {
            break;
        }
    }

    System.out.println("\n");
    System.out.println("First integer entered was : " + input.get(0));
    System.out.println("Second integer entered was : " + input.get(1));
最终扫描仪控制台=新扫描仪(System.in);
最终列表输入=新的ArrayList();
while(true)
{
System.out.print(“请输入一个整数:”);
if(console.hasNextInt())
{
input.add(console.nextInt());
}
其他的
{
System.out.println(“输入错误,请重试…”);
console.next();
}
如果(input.size()>=2)
{
打破
}
}
System.out.println(“\n”);
System.out.println(“输入的第一个整数是:“+input.get(0));
System.out.println(“输入的第二个整数是:“+input.get(1));

另一种使用Scanner#hasnetint()验证输入的解决方案:

    final Scanner console = new Scanner(System.in);
    final List<Integer> input = new ArrayList<Integer>();

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

        if (console.hasNextInt())
        {
            input.add(console.nextInt());
        }
        else
        {
            System.out.println("Bad input, try again ...");
            console.next();
        }
        if (input.size() >= 2)
        {
            break;
        }
    }

    System.out.println("\n");
    System.out.println("First integer entered was : " + input.get(0));
    System.out.println("Second integer entered was : " + input.get(1));
最终扫描仪控制台=新扫描仪(System.in);
最终列表输入=新的ArrayList();
while(true)
{
System.out.print(“请输入一个整数:”);
if(console.hasNextInt())
{
input.add(console.nextInt());
}
其他的
{
System.out.println(“输入错误,请重试…”);
console.next();
}
如果(input.size()>=2)
{
打破
}
}
System.out.println(“\n”);
System.out.println(“输入的第一个整数是:“+input.get(0));
System.out.println(“输入的第二个整数是:“+input.get(1));

另一种使用Scanner#hasnetint()验证输入的解决方案:

    final Scanner console = new Scanner(System.in);
    final List<Integer> input = new ArrayList<Integer>();

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

        if (console.hasNextInt())
        {
            input.add(console.nextInt());
        }
        else
        {
            System.out.println("Bad input, try again ...");
            console.next();
        }
        if (input.size() >= 2)
        {
            break;
        }
    }

    System.out.println("\n");
    System.out.println("First integer entered was : " + input.get(0));
    System.out.println("Second integer entered was : " + input.get(1));
最终扫描仪控制台=新扫描仪(System.in);
最终列表输入=新的ArrayList();
while(true)
{
System.out.print(“请输入一个整数:”);
if(console.hasNextInt())
{
input.add(console.nextInt());
}
其他的
{
System.out.println(“输入错误,请重试…”);
console.next();
}
如果(input.size()>=2)
{
打破
}
}
System.out.println(“\n”);
System.out.println(“输入的第一个整数是:“+input.get(0));
System.out.println(“输入的第二个整数是:“+input.get(1));

另一种使用Scanner#hasnetint()验证输入的解决方案:

    final Scanner console = new Scanner(System.in);
    final List<Integer> input = new ArrayList<Integer>();

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

        if (console.hasNextInt())
        {
            input.add(console.nextInt());
        }
        else
        {
            System.out.println("Bad input, try again ...");
            console.next();
        }
        if (input.size() >= 2)
        {
            break;
        }
    }

    System.out.println("\n");
    System.out.println("First integer entered was : " + input.get(0));
    System.out.println("Second integer entered was : " + input.get(1));
最终扫描仪控制台=新扫描仪(System.in);
最终列表输入=新的ArrayList();
while(true)
{
System.out.print(“请输入一个整数:”);
if(console.hasNextInt())
{
input.add(console.nextInt());
}
其他的
{
System.out.println(“输入错误,请重试…”);
console.next();
}
如果(input.size()>=2)
{
打破
}
}
System.out.println(“\n”);
System.out.println(“输入的第一个整数是:“+input.get(0));
System.out.println(“输入的第二个整数是:“+input.get(1));

检查这个,使用正则表达式如何?你说“我真的不想使用异常和try/catch语句,因为我不理解它们”是什么意思如果你想进入编程领域,你必须了解异常。我知道我刚刚知道会抛出哪些异常,然后我将如何使用错误。这是解决这个问题的好方法吗?检查这个,使用正则表达式如何?你的意思是什么