How to accept single character input from user in Java?

In the example below, I am trying to accept one character of input from the user, but when I run the program, I get a do..while loop that runs multiple times. See Program Result below.

If someone can help me with an answer, how to solve this problem?

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');

    }

}

      

Output

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' 

      

+3


source to share


6 answers


Using DataInputStream is probably not the best way to handle your situation.

The DataInputStream buffers the inputs you enter, so you get unwanted long messages with loops.



Try using a scanner instead. Here's an example of a scanner:

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

      

+2


source


Use System.in.read () instead of DataInputStream.



+2


source


DataInputStream

( InputStream

) is essentially a binary construct . If you want to read text data (from the console for example), you must use Reader. The source code has been commented out BufferedReader

. Better to use the same instead DataInputStream

. You can do as below,

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

      

Or you can use it DataInputStream.readLine()

, but it's deprecated for a reason. And they suggested using it there as well BufferedReader.readLine()

.

You can use other options like Scanner .

+1


source


User Scanner class for inputting input. Its a good way to solve your problem.

Scanner scan = new Scanner(System.in);
System.out.println(scan.next().charAt(0));

      

+1


source


Since this thread does not have an ACCEPTED ANSWER, I will answer anyway even though it is really old; for people who still want an explanation.

Usage Scanner

is the best idea for the situation. The scanners are easy to use and stop filament before completion. You can use this to your advantage or create a new thread to keep the current thread running. Basic use of the 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" will return EVERYTHING before the space in the scanner. To find only the first letter, split the string by ""

, then take the 1st (0th) array.

Finally, a good quote from this thread about scanners

next () can only read input up to a space. It cannot read two words separated by a space. Additionally, next () puts the cursor on the same line after reading the input.

nextLine () reads input, including space between words (that is, it reads to the end of the line \ n). After entering input, nextLine () positions the cursor on the next line.

Later, when searching for multiple words, use nextLine () instead of next () to get the full line

0


source


Perhaps in the above run example, you specified input as "asdfgaf" and then pressed enter. hence it accepts A, S, D, F .... as input one character at a time. you must specify the input as "y" or "n" or "e" (one character at a time) and then press "Enter".

eg If you want to access your account, if yes, enter "Y" or if you want to create a new account, press "N" to exit press "E": y

-1


source







All Articles