System.in.read() method

后端 未结 6 1360
太阳男子
太阳男子 2020-12-02 03:03

Following is the code, I have written to get two inputs from user. But when I run the program, It takes only one input and generate other by itself and calculate the wrong v

相关标签:
6条回答
  • 2020-12-02 03:33

    hey bro this should work for what you need i think if its not let me know

    import java.io.IOException;
    import java.util.Scanner;
    
    public class noob{
    
    
    class ThrowsExcpt {
        int divide(int x, int y) throws ArithmeticException, IOException {
            return x / y;
        }
    }
    
    class ThrowsTemps {
        public void main(String s[]) throws IOException {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number 1");
            int x = sc.nextInt();
     System.out.println("Enter number 2");
            int y = sc.nextInt();
    
    0 讨论(0)
  • 2020-12-02 03:39

    System.in is an InputStream - read() reads exactly one byte. Your direct input is more than one byte and so both values are directly read within the first input.

    Use a Scanner instead (with System.in as Input):

     Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();
    

    More Examples: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

    0 讨论(0)
  • 2020-12-02 03:39

    Why unwanted assignment is occuring?

    The keyboard input character(s) is stored in input buffer after ENTER is pressed. ENTER includes line feed '\n' in the input character. Decimal/integer value of line feed '\n' is 10.

    System.in.read() reads only first one character from the input character(s), the character is casted into integer and return it. And that character will be excluded from the input buffer.

    The rest of the characters are still pending in the input buffer until it is read by System.in.read() in the same manner stated above.

        int inputchar1, inputchar2, inputchar3;
    
        System.out.print("input: ");// input: 43
    
        // The line feed '\n' is generated when ENTER is pressed.
        // '4', '3' and '\n' are in the input buffer.
    
        inputchar1 = System.in.read(); // '4' is read and returns 52 (corresponding integer value)
        // '3' and '\n' is in the input buffer.
    
        inputchar2 = System.in.read(); // '3' is read and returns 51
        // '\n' is pending in the input buffer.
    
        inputchar3 = System.in.read(); // '\n' is read and returns 10
        // no more input character in the input buffer.
    
        System.out.println("inp1 =" + inputchar1);
        System.out.println("inp1 =" + inputchar2);
        System.out.println("inp1 =" + inputchar3);
    
        /*
         * Refer to ASCII table for char to decimal conversion.
         * Although java Char is 16-bit unicode ASCII is still applicable.
         */
    

    How to avoid unwanted assignment?

    Keep reading the content of input buffer until it gets depleted.

    int x, y, avoid;
    
    System.out.println("Enter x: ");
    x = System.in.read();
    // Reading all the characters after the first character so that
    // no character stays pending in the input buffer.
    do {
        avoid = System.in.read();
    } while(avoid != '\n');
    
    System.out.println("Enter y: ");
    // New input buffer is created.
    y = System.in.read();
    do {
        avoid = System.in.read();
    } while(avoid != '\n');
    

    Reference: "Java: A beginner's guide 6e" - Herbert Schildt

    0 讨论(0)
  • 2020-12-02 03:43

    Try using a Scanner object to read your user's input :

    Scanner scanner = new Scanner(System.in);
    int x = scanner.nextInt();
    
    0 讨论(0)
  • 2020-12-02 03:44

    simply put -

    int x = System.in.read();
    

    automatically throws an exception.

    • you allocated and defined a memory cell to hold an int, then assigned a byte to be stored there. Recast the input or use a diff. input method.
    0 讨论(0)
  • 2020-12-02 03:56

    Read() method.

    Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

    from http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29.

    So use scanner for this.

    And according to your question Why is it not asking for second input? Reason is read() method wait for input source once it gets the input it takes the byte by byte from that input source. For ex:

            inChar = System.in.read();
            i = System.in.read();
            i1 = System.in.read();
            System.out.print("You entered ");
            System.out.println(inChar);
            System.out.println(i);
            System.out.println(i1);
    

    you enter abcas input then you will get

    Enter a Character:
    abc
    You entered 97
    98
    99
    

    as out put which is byte values of a,b and c.

    Explanation: input abc byte array

    +--------------------+
    | 97 | 98 | 99 | other byte value for **Enter**
    +--------------------+ 
    

    first System.in.read() will get first index of array, second one will get second and so on.

    0 讨论(0)
提交回复
热议问题