如何限制扫描仪的输入?

时间:2014-07-19 00:11:07

标签: java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int inputInt = checkInput(in, "Enter an integer and a base: ");
    int inputBase = checkInput(in, "");
}

public static int checkInput(Scanner in, String prompt) {
    System.out.print(prompt);
    while (!in.hasNextInt()) {
        in.next();
        System.out.println("Sorry, that is an invalid input.");
        System.out.print(prompt);
    }
    return in.nextInt();
}

此方法有效且不会返回任何错误输入,即; p“你好”。

我的问题是如何限制扫描仪读取的输入数量。假设我输入了5 five % ;,但我只希望将5five传递给我的方法,其余的都会丢弃。

我查看了Java API但找不到限制接受的用户输入量的方法。我只是错过了它还是有另一种方法可以做到这一点?

编辑:我尝试使用.length()方法来限制输入,但是不允许大于.length()参数的整数。

4 个答案:

答案 0 :(得分:2)

Scanner sc= new Scanner(System.in);
String string = sc.findInLine(".{500}"); // length of your input you want 

findInLine(字符串模式)

扫描程序类java.util package的方法。此方法返回一个String对象,该对象满足指定为方法参数的模式。

see this article

答案 1 :(得分:1)

如果您只想获取前两个单词(或由空格分隔的字符串),则可以使用str.split(" ");方法。 例如:

String input = in.nextLine(); // Gets the next line the user enters (as a String)
String[] inputWords = input.split(" "); // inputWords[0] is first word, inputWords[1] 
                                        // is second word... etc
String validInput = inputWords[0] + " " + inputWords[1]; // Combines the first and 
// second words into a string, so if you had "5 five %" validInput would be "5 five"
// inputWords[0] is "5", inputWords[1] is "five", inputWords[3] is "%" etc for any other words...

这将基本上限制两个单词的输入数量。

我希望这有帮助!

答案 2 :(得分:1)

以下是如何完成所需工作的工作示例。我将其分解,以便为每个输入提示用户一次,这样可以更容易验证。我将checkInput方法更改为getInput,仅将有效用户输入作为String返回,然后将其转换为int

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int inputInt = Integer.parseInt(getInput(in, "Enter an integer: "));
    int inputBase = Integer.parseInt(getInput(in, "Enter a base: "));
    System.out.println("Int: " + inputInt + ", base: " + inputBase);
}

public static String getInput(Scanner in, String prompt) { // Get valid user input
    System.out.print(prompt); // Tell user what to input
    String text = "";
    while (true) { // Keep looping until valid input is found
        text = in.nextLine(); // Get input from stdin
        if(isInteger(text)) // Check if they put in integer
            break; // Exit loop
        System.out.print("Try again, " + prompt); // Wasn't valid, prompt again
    } 
    return text; // Return valid user input
}

private static boolean isInteger(String str) { // Check if string is integer
    try {
        Integer.parseInt(str); // If this doesn't fail then it's integer
        return true;
    } catch(NumberFormatException e) {
        return false; // Wasn't integer
    }
}

示例运行:

Enter an integer: 2 dog five 3
Try again, Enter an integer: 2
Enter a base: cat
Try again, Enter a base: 3
Int: 2, base: 3

它有助于分离功能 - 您尝试在一种方法中读取输入,验证输入并转换为int。如果你打破它,它就会变得更容易管理。

答案 3 :(得分:1)

扫描仪扫描=新扫描仪(System.in);

    System.out.println ("enter a 2 numbers");

    String s;

    s = scan.nextLine();

    Scanner scan2 = new Scanner(s);

    int one = scan2.nextInt();
    int two = scan2.nextInt();

    System.out.println (" int 1 = " + one + " int 2 = " + two);

输入2个数字 23 45 68 96 45  int 1 = 23 int 2 = 45

流程已完成。