【问题标题】:can't properly read in characters using 'Scanner' and '.next().charAt(0)'无法使用“扫描仪”和“.next().charAt(0)”正确读取字符
【发布时间】:2017-12-29 00:22:53
【问题描述】:

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

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

1) 当我输入一个字符时,我收到以下错误消息:"You entered a java.util.Scanner[delimiters=\p{javaWhitespace}+][position=1][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q8\E]"

2) 当我按下空格键时,我在输入句号之前没有收到任何反馈,然后我收到类似于上面的错误消息,但程序确实停止了。

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

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

我猜问题出在keystroke = 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();
  }
}

【问题讨论】:

  • else System.out.println("You entered a " + userInput); ---> 将其更改为 else System.out.println("You entered a " + keystroke);

标签: java java.util.scanner


【解决方案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");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多