Validate user input to only accept type int or Str

2019-07-28 05:11发布

My code prints out an array which is already declared then asks for user input. User is supposed to enter a number in the format xy or type quit to stop using the program. After getting user input it prints out the element of the array using x as row and y as column number which is followed by setting that index to 0 and printing the new array. I have so far achieved most of it apart from accepting only integers or "quit" from the user. If user enters another string apart from "quit" the program crashes. This is my code. 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);

    }
}

The problem is in this bit

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;

I get this error "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.substring(String.java:1963) at Exercise23.main(Exercise23.java:26) "

4条回答
Evening l夕情丶
2楼-- · 2019-07-28 05:22

The problem with your code is that any string that is not "quit" will enter the first if condition, this is what causes your code to crash. You only want to enter the fist condition if the string supplied is numeric. What your conditions should be are:

if (StringUtils.isNumeric(xy)) {
...
} else if (!xy.equals("quit")) {
    System.out.println("You can only enter integers or 'quit'.");
} else {
    exitCon= true;
}
查看更多
forever°为你锁心
3楼-- · 2019-07-28 05:25

Please see the code, this will cover the cases and is simple extendable:

        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'.");
            }
        }
查看更多
Lonely孤独者°
4楼-- · 2019-07-28 05:33

With the exception added, it appears the failure is occurring here;

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

The StringIndexOutOfBoundsException means the String "xy" is too short, presumably blank on the readline.

查看更多
对你真心纯属浪费
5楼-- · 2019-07-28 05:37

Thanks everyone. I have managed to solve my own problems with ideas from your answers and comments.

I made these changes

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'.");
查看更多
登录 后发表回答