0

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 value. please help. Thanks

import java.io.IOException;
import java.util.*;

class ThrowsExcpt {
    int divide(int x, int y) throws ArithmeticException, IOException {
        return x / y;
    }
}

class ThrowsTemps {
    public static void main(String s[]) throws IOException {

        int x = System.in.read();
        int y = System.in.read();

        ThrowsExcpt th = new ThrowsExcpt();
        int r = th.divide(x, y);
        System.out.println(r);
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
Ashish Biyahut
  • 41
  • 3
  • 3
  • 7

6 Answers6

5

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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
matt
  • 153
  • 7
  • if i have written the read() method two times. Why it is not getting two inputs. – Ashish Biyahut Mar 28 '14 at 09:19
  • because one single byte/character is read by one read() - so if you input 12 it is read as (character) 1 and 2 - therefore it fullfills two read() calls ('1' is 49 and '2' is 50) - with Scanner and nextInt you already get the correct value and not the character code – matt Mar 28 '14 at 11:34
  • But i have entered as input 4 only. It takes other input itself. and does calculation – Ashish Biyahut Mar 28 '14 at 12:25
  • Yes, the return is read as second input! - return should be 13, and the code of 4 is 52 - so you calculate 52/13 – matt Mar 28 '14 at 13:55
  • Scanner is extremely slow – Pavel Mar 02 '18 at 06:42
1

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

Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Savv
  • 433
  • 2
  • 7
1

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.

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
1

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

gaidink
  • 35
  • 1
  • 8
  • 1
    Instead of referring to an ASCII table, here's a more relevant reference [Unicode Nameslist](http://www.unicode.org/charts/nameslist/index.html). – Tom Blodget Mar 19 '17 at 16:35
0

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();
Micha
  • 5,117
  • 8
  • 34
  • 47
HelloWorld
  • 133
  • 1
  • 15
  • Thanks for the code, But I want two user input with the help of read() method only. May I take two user inputs using console with the help of read() method. Kindly suggest. – Ashish Biyahut Mar 28 '14 at 09:28
0

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.
Tapper7
  • 173
  • 8