繁体   English   中英

验证用户输入只接受int或String“quit”类型

[英]Validate user input to only accept type int or String “quit”

我的代码打印出一个已经声明的数组然后要求用户输入。 用户应输入格式为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;

我得到此错误“线程中的异常”主“java.lang.StringIndexOutOfBoundsException:String index索引超出范围:1在java.lang.String.substring(String.java:1963)在Exercise23.main(Exercise23.java:26) “

除了添加的例外,这里似乎发生了失败;

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

StringIndexOutOfBoundsException意味着字符串“xy”太短,在readline上可能是空白的。

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

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

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

我做了这些改变

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'.");

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

        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'.");
            }
        }

暂无
暂无

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

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