验证用户输入只接受类型int或String"退出"

时间:2016-10-16 21:41:18

标签: java validation input user-input

我的代码打印出一个已经声明的数组然后要求用户输入。用户应输入格式为xy的数字或键入quit以停止使用该程序。在获得用户输入后,它使用x作为行打印出数组元素,使用y作为列号,然后将该索引设置为0并打印新数组。 到目前为止,除了只接受整数或者退出"来自用户。如果用户输入另一个字符串,除了"退出"程序崩溃了。 这是我的代码。     import java.util.Scanner;

public class Exercise23 {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);

        int [][] array = {
            {0, 1, 4, 5},
            {3, 7, 9, 7},
            {1, 8, 2, 1}
        };

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }

        boolean exitCon = false;

        do {
            System.out.println("Please enter a number in the format 'xy' with no spaces in between or enter 'quit' to stop");
            String xy = read.nextLine();
            if (!"quit".equals(xy)) {
                String x = xy.substring(0, 1);
                String y = xy.substring(1);
                int row = Integer.parseInt(x);
                int column = Integer.parseInt(y);
                if (0 <= row && 0 <= column && row <= 2 && column <=) {
                System.out.println();
                    System.out.println(array[row][column]);
                    array[row][column] = 0;

                    System.out.println();

                    for (int i = 0; i < array.length; i++) {
                        for (int j = 0; j < array[i].length; j++) {
                            System.out.print(array[i][j]);
                        }
                        System.out.println();
                    }
                    System.out.println();

                } else { System.out.println("The number has to be in range 00-23 inclusive considering the format 'xy'.");

                }

            } else if (xy.equals("")) {
                System.out.println("You can only enter integers or 'quit'.");               
            } else {
                exitCon= true;   
            }

        } while (!exitCon);

    }
}

问题在于此位

String xy = read.nextLine();
if (!"quit".equals(xy)) {
    String x = xy.substring(0, 1);
    String y = xy.substring(1);
    int row = Integer.parseInt(x);
    int column = Integer.parseInt(y);
    if (0 <= row && 0 <= column && row <= 2 && column <= 3) {
        System.out.println();
        System.out.println(array[row][column]);
        array[row][column] = 0;

        System.out.println();

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
        System.out.println();

        } else { System.out.println("The number has to be in range 00-23 inclusive considering the format 'xy'.");

        }

    } else if (xy.equals("")) {
            System.out.println("You can only enter integers or 'quit'.");               
    } else {
        exitCon= true;

我收到此错误&#34;线程中的异常&#34; main&#34; java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1     at java.lang.String.substring(String.java:1963)     在Exercise23.main(Exercise23.java:26) &#34;

4 个答案:

答案 0 :(得分:0)

添加例外后,似乎发生了失败;

String x = xy.substring(0, 1);
String y = xy.substring(1);

StringIndexOutOfBoundsException表示String&#34; xy&#34;太短了,可能在空白处就是空白。

答案 1 :(得分:0)

您的代码存在的问题是任何不是&#34;退出&#34;将进入第一个if条件,这是导致代码崩溃的原因。如果提供的字符串是数字,您只想输入第一个条件。你的条件应该是:

if (StringUtils.isNumeric(xy)) {
...
} else if (!xy.equals("quit")) {
    System.out.println("You can only enter integers or 'quit'.");
} else {
    exitCon= true;
}

答案 2 :(得分:0)

谢谢大家。我已经设法通过你的答案和评论来解决我自己的问题。

我做了这些修改

if (xy.length() == 2) {
    String x = xy.substring(0, 1);
    String y = xy.substring(1);
    int row = Integer.parseInt(x);
    int column = Integer.parseInt(y);
    if (0 <= row && 0 <= column && row <= 2 && column <= 3) {
    System.out.println();
        System.out.println(array[row][column]);
        array[row][column] = 0;

        System.out.println();

        for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    System.out.print(array[i][j]);
                }
                System.out.println();
        }
        System.out.println();

        } else { System.out.println("The number has to be in range 00-23 inclusive considering the format 'xy'.");

        }

} else if ("quit".equals(xy)) {
        exitCon= true;                              
} else {
        System.out.println("You can only enter integers or 'quit'.");

答案 3 :(得分:0)

请参阅代码,这将涵盖案例并且可以简单扩展:

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in,"UTF-8"));
        String s;
        while ((s=br.readLine())!=null) {
            if (s.equals("quit")) {
                break;
            }
            else if (s.matches("\\d{2}")) {
                // parse it (exactly 2 digits)
                System.out.print("parse:"+s);
            }
            else {
                System.out.println("You can only enter 2dig integers or 'quit'.");
            }
        }