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

标签 java java.util.scanner

注意:我在 Netbeans 8.2 和 Windows 7 上运行此程序

该程序要求用户输入,他们可以输入一个字符,按空格键,或输入一个句点来停止程序。

1) 当我输入字符时,收到以下错误消息:“您输入了 java.util.Scanner[delimiters=\p{javaWhitespace}+][position=1][match valid=true] [需要输入=假][源关闭=假][跳过=假][组分隔符=\,][小数分隔符=\.][正前缀=][负前缀=\Q-\E][正后缀= ][负数后缀=][NaN字符串=\Q?\E][无穷大字符串=\Q8\E]"

2)当我点击空格键时,我没有得到任何反馈,直到我输入一个句点,然后我收到类似于上面的错误消息,但程序确实停止了。

3)如果我输入句点,我也会收到类似的错误消息,但程序确实会停止。

我期待的是以下内容: a)如果我按空格键,它会返回一条消息,说明我按空格键并增加两个计数器 b) 如果我输入一个字符,它会返回一条消息,说明输入的字符并增加 ctr 计数器 c) 如果输入句点,则返回一条消息,说明该消息加上停止程序的次数

我猜问题出在 keytrip = userInput.next().charAt(0); 语句上。我认为使用 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();
  }
}

最佳答案

您必须使用 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");
        }
    }
}

关于java - 无法使用 'Scanner' 和 '.next().charAt(0)' 正确读取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45263248/

相关文章:

java - 扫描仪和文本中的字母 "Ł"出现问题 - JAVA

java - 如何解决使用GluonHQ客户端、Native Image和GraalVM编译的JavaFX项目中的fxml加载异常?

java - Log4j - 有时只需要换行

java - 如何在SWT中设置最大复合尺寸

java - 将多个对象序列化为一个文件并稍后检索它们

java - 如何循环直到用户输入有效整数?

java - 当输入字符/字符串而不是整数时输出错误

java - IText:如何在pdf中添加空白页?

java - 在 Java 中使用 "OR"运算符

java - 我正在做一个 do while 循环,它要求用户输入 ID,但在用户输入任何内容之前,它总是会在一个循环之后显示错误消息