[英]can't properly read in characters using 'Scanner' and '.next().charAt(0)'
注意:我正在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();
}
}
您必须使用nextLine()
而不是next()
来读取空格。 在此处查看更多详细信息: 扫描仪看不到空格 。 使用isSpaceChar
来检查带有变量的空间。 在此处查看更多详细信息: 检查字符属性 。 正确的代码是...。
/* 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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.