无法使用'Scanner'和'.next()正确读取字符.charAt(0)'

时间:2017-07-23 08:50:37

标签: java java.util.scanner

注意:我在Netbeans 8.2和Windows 7上运行它

此程序要求用户输入,他们可以输入字符,点击空格键,或输入一段时间来停止程序。

1)当我输入一个字符时,我收到以下错误消息:ctr

2)当我点击空格键时,在我输入一段时间之前我得不到任何反馈,然后我收到类似于上面的错误消息,但程序确实停止了。

3)如果我输入一段时间,我也会收到类似的错误信息,但程序会停止。

我期待的是以下内容: a)如果我按空格键,它会返回一条消息,说我按了空格键并递增两个计数器 b)如果我输入一个字符,那么它会返回一条消息,说明输入的字符并递增keystroke = userInput.next().charAt(0);计数器 c)如果输入句点,则返回一条消息,说明加上停止程序的次数

我猜测问题出在userInput.next().charAt(0)语句中。我认为使用/* reads a char, a space, or a period from keyboard, returns user input, counts number of spaces and total number of entries */ package ch03_36_exercise_01; import java.util.Scanner; public class Ch03_36_Exercise_01 { public static void main(String args[]) throws java.io.IOException { Scanner userInput = new Scanner(System.in); char keystroke; // character that user enters int ctr = 0, spaces = 0; // num of tries to stop run, num of spaces entered do { // ask for user input System.out.print("Enter a character, or hit the space bar," + " or enter a period to stop: "); keystroke = userInput.next().charAt(0); if (keystroke == ' ') { System.out.println("You entered a space"); spaces++; // increment space bar count } else System.out.println("You entered a " + keystroke); // increment keystroke count ctr++; } while (keystroke != '.'); System.out.print("It took " + ctr + " tries to stop"); if (spaces > 0) System.out.println(" and you hit the space bar " + spaces + " times\n"); else System.out.println(); } } 会起作用,因为它们都是单击键和字符。空间是一个角色,对吗?错误?因此,如果有人能指出我正确的方向来解决这个问题,那将是值得赞赏的。

print_login_to_see()

1 个答案:

答案 0 :(得分:1)

您必须使用nextLine()代替next()来阅读空格。在此处查看更多详细信息:Scanner doesn't see after space。使用isSpaceChar检查带变量的空间。在此处查看更多详细信息:Checking Character Properties。更正后的代码是....

/* reads a char, a space, or a period from keyboard, returns user input,
   counts number of spaces and total number of entries */
package ch03_36_exercise_01;

import java.util.Scanner;

public class Ch03_36_Exercise_01 {

    public static void main(String args[]) throws java.io.IOException {

        Scanner userInput = new Scanner(System.in);
        char keystroke;           // character that user enters
        int ctr = 0, spaces = 0;  // num of tries to stop run, num of spaces entered

        do {
            // ask for user input
            System.out.print("Enter a character, or hit the space bar,"
                    + " or enter a period to stop: ");
            keystroke = userInput.nextLine().charAt(0);

            if (Character.isSpaceChar(keystroke)) {
                System.out.println("You entered a space");
                spaces++; // increment space bar count
            } else {
                System.out.println("You entered a " + keystroke);
            }

            // increment keystroke count
            ctr++;
        } while (keystroke != '.');

        System.out.print("It took " + ctr + " tries to stop");

        if (spaces > 0) {
            System.out.println(" and you hit the space bar " + spaces + " times\n");
        }
    }
}