验证用户输入以仅接受类型int或字符串"quit". [英] Validate user input to only accept type int or String "quit"

查看:105
本文介绍了验证用户输入以仅接受类型int或字符串"quit".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

    }
}

问题出在这一点

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:字符串索引超出范围:1 在java.lang.String.substring(String.java:1963) 在Exercise23.main(Exercise23.java:26) "

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) "

推荐答案

请查看代码,它将涵盖所有情况并且可以轻松扩展:

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

这篇关于验证用户输入以仅接受类型int或字符串"quit".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆