Enter data without using anything other than System.in.read ()

I find it difficult to find information on how System.in.read(); works System.in.read(); maybe someone can help me. A scanner seems to be preferable, but I am not allowed to use it.

I was assigned a task in which I had to read the user input of the console in the form of Boolean-Operator-Boolean, for example. T ^ F or T & T via System.in.read() and just print what the operator returns.

Normal people are likely to use a different method, but the assignment specifically indicates that only System.in.read() and System.out.println() allowed.

Here is my attempt to solve it:

 import java.io.*; public static void main(String[] args) { String error = "Reading error, please use T or F"; boolean a = true; //char: 84 or 116 for T and t boolean b = false; //char: 70 or 102 for F and f int userChar1; int userOperator; int userChar2; int chosenOperator = 0; try { //Get first char System.out.println("Enter the first value (T or F):"); userChar1 = System.in.read(); if((userChar1==84)||(userChar1==116)) { // T or t a = true; } else if ((userChar1==70)||(userChar1==102)) { // F or f a = false; } else { System.out.println(error); } //Get second char System.out.println("Select an operator: & | ^"); userOperator = System.in.read(); if(userOperator==38) { // & chosenOperator = 0; } else if (userOperator==124) { // | chosenOperator = 1; } else if (userOperator==94) { // ^ chosenOperator = 2; } else { System.out.println(error); } //Get third char System.out.println("Enter the second value:"); userChar2 = System.in.read(); System.in.close(); if((userChar2==84)||(userChar2==116)) { b = true; } else if ((userChar2==70)||(userChar2==102)) { b = false; } else { System.out.println(error); } //Figure out result boolean result; switch (chosenOperator) { case 0: result = a&b; case 1: result = a|b; case 2: result = a^b; System.out.println(result); } } catch(IOException e) { } } 

The execution of this code causes the console to wait for user input after the first System.in.read() and checks that the char input is correct. After that, all subsequent System.in.read() ignored and the program terminates.

I found a piece of code that used System.in.close() , so I still don’t know which methods I spliced ​​after each System.in.read() . This causes the program to terminate when the first System.in.read() is called after.

So what exactly is going on? How do you use System.in.read() correctly?

+4
source share
3 answers

The problem is not in System.in.read (), but in the console. The console is usually buffered, which means that the data is sent to your program (and thus System.in.read () is read) only after pressing the enter key. Therefore, you will have to switch the console to unbuffered mode, but there is no portable way to do this because there are so many different types of consoles (unix shell, cmd window, Eclipse console, etc.).
If you are forbidden to use anything other than the System.in.read () and System.out.println () methods, you must allow the user to enter the full term in the line, press enter, and then you can process the characters entered, for example:

 public static void main(String[] args) throws IOException { boolean a = true; // char: 84 or 116 for T and t boolean b = false; // char: 70 or 102 for F and f int userChar1; int userOperator; int userChar2; System.out.println("Please enter the term, eg T&F:"); userChar1 = System.in.read(); userOperator = System.in.read(); userChar2 = System.in.read(); a = userChar1 == 'T' || userChar1 == 't'; b = userChar2 == 'T' || userChar1 == 't'; switch (userOperator) { case '&': System.out.println(a & b);break; case '|': System.out.println(a | b);break; case '^': System.out.println(a ^ b);break; default: System.out.println("unknow operator"); } } 
+3
source

Take a look at the following part of the code:

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = null; // read the input from the command-line; need to use try/catch with the // readLine() method try { input = br.readLine(); } catch (IOException e) { System.out.println("IO error trying to read your input!"); System.exit(1); } System.out.println("your input: "+input); 

If you are not allowed to use bufferReader, I am sure that you are looking at the source code of bufferReader.

+1
source

Personally, I had no problem running your code, but it had some weird effects that I had to learn.

The main problem with this approach (using System.in.read() ) is that when the user enters a character, he must press enter , and therefore you have 2 characters instead of 1 that you expect - the first character that the user enters (for example, T or F in your case) and the new character of the string.

I managed to get your code to work with minor changes. You need to call the overloaded version of the read() method - read(byte[] b) , which reads some number of bytes from the input stream and stores them into the buffer array .

Here is the first part of your code, modified:

 byte[] input = new byte[10]; System.out.println("Enter the first value (T or F):"); System.in.read(input); userChar1 = input[0]; if ((userChar1 == 84) || (userChar1 == 116)) { // T or t a = true; } else if ((userChar1 == 70) || (userChar1 == 102)) { // F or f a = false; } else { System.out.println(error); } 

Note that we do not need to return read() , we only care about the first char array from input . The rest of the code changes in the same way, you can use the same buffer array ( input ) and the query for the 0th element.

+1
source

Source: https://habr.com/ru/post/1442607/


All Articles