如何在Java中从用户处获取单个字符输入?

4
在下面的例子中,我试图从用户接受单个字符输入,但是运行程序时,do..while循环执行了多次。请查看以下程序的结果。
如果有人能帮我解决这个问题,请告诉我如何修复它?
import java.io.*;



public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {



            char c;
           // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            DataInputStream in =new DataInputStream(System.in);

            // Asking the user what to do with the application
           do{ 

            System.out.println("Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' ");          

            byte b = in.readByte();
            c = (char) b;
            c = Character.toUpperCase(c);

            if (c=='Y'){
                System.out.println(c);
               }
            else if (c=='N') {
              System.out.println(c);
            }
            else if (c=='E'){
               System.out.println(c); 
            }
            else{
                System.out.println("Incorrect Entry, try again: "+c); 
            }

           }while (c!='E');

    }

}

输出

init:
deps-jar:
compile:
run:
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
asdfgaf
Incorrect Entry, try again: A
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: S
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: D
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: F
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: G
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: A
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: F
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: 

Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 

1
也许你可以使用readLine()代替readByte()。 - Bob Flannigon
为什么你不期望while循环执行多次?这就是循环的目的,而你的循环会一直循环直到c == 'E'。由于你似乎没有输入E,所以这种行为看起来正如我所期望的那样。如果你能解释一下你期望的替代行为,那将会很有帮助。 - Andrzej Doyle
6个回答

2

使用DataInputStream可能不是处理您的情况的最佳方式。

DataInputStream缓冲输入的内容,这就是为什么您在循环中得到不想要的冗长消息的原因。

尝试改用Scanner。以下是Scanner的示例:

Scanner objScanner = new Scanner(System.in);
char c = objScanner.next().charAt(0);
System.out.println(c);

使用charAt方法可以有效地将输入修剪为仅包含第一个字符,但在某些情况下,如果同时T和Y都是有效选择,则放置"ty"应该产生错误而不是返回't'。特别是如果用户意外按下了两个键,但打算写Y的情况。 - Steen Schütt

2

使用System.in.read()代替DataInputStream。


2
用户扫描器类用于获取输入。这是解决您问题的好方法。
Scanner scan = new Scanner(System.in);
System.out.println(scan.next().charAt(0));

1

由于这个线程还没有接受的答案,即使它非常旧,我仍然会回答那些仍然需要解释的人。

对于这种情况,使用Scanner是最好的选择。Scanner易于使用,并在完成之前停止线程。你可以利用这一点,或者你可以创建一个新的线程来保持当前线程运行。Scanner的基本用法:

Scanner s = new Scanner(System.in) //Makes a new scanner with System.in
String letter = s.next(); //Use next to find the character typed
//Insert code using letter variable

"letter" 会返回空格之前的所有内容。如果只想要第一个字母,可以通过使用""来分割字符串,然后选择数组中的第1个(0th)字符。

最后,我们可以从 this thread about scanners 中获取一句好的引用。

next() 只能读取输入直到空格。它不能读取由空格分隔的两个单词。此外, next() 在读取输入后将光标放在同一行上。

nextLine() 可以读取包括单词之间的空格在内的输入(即,它会一直读取到结尾的 \n 符号)。一旦读取了输入, nextLine() 会将光标定位在下一行。

稍后,在寻找多个单词时,请使用 nextLine() 而不是 next() 来获取完整的字符串。


1

DataInputStreamInputStream)本质上是一个二进制结构。如果您想读取文本数据(例如从控制台),则应使用Reader。在源代码中,有一个被注释掉的BufferedReader。最好使用它来替代DataInputStream。您可以按照以下方式操作,

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine();

或者您可以使用DataInputStream.readLine(),但是它已经过时了。建议使用BufferedReader.readLine()

您还可以选择其他选项,如Scanner


-1

可能在你提供的执行示例中,你输入了"asdfgaf",然后按下了回车键。 因此它会将A、S、D、F……作为一个字符一次输入。 你应该输入'y'或'n'或'e'(每次只能输入一个字符),然后按回车键。

例如: 您是否要访问您的帐户?如果是,请输入“Y”;如果您想创建新帐户,请按“N”退出,请按“E”:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接