1

Code:

public class Adddemo {

    public static void main(String[] args) throws IOException {
        int i, j, k;
        System.out.println("enter value of i: ");
        i = (int) System.in.read();
        System.out.println("enter value of j: ");
        j = (int) System.in.read();

        k = i + 1;

        System.out.println("sum is: " + k);
    }
}

Is System.in.read used for multiple inputs?

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Max410
  • 51
  • 1
  • 10

4 Answers4

3

System.in.read() is used to read a character.

suppose you enter 12, then i becomes 1(49 ASCII) and j becomes 2(50 ASCII).

suppose you enter 1 and then press enter, i becomes (ASCII 49) and enter(ASCII 10), even enter is considered a character and hence skipping your second input.

use a scanner or bufferedReader instead.

Using a scanner

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
int k = i + j; 
System.out.println(k);

Using a BufferedReader

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(reader.readLine());
int j = Integer.parseInt(reader.readLine());
int k = i + j;
System.out.println(k);
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • If u print only value of j than it displays 10. and also it doesn't even ask for the input of j... thats confusing to me.. – Max410 Jun 11 '15 at 13:01
  • because 10 is the ASCII value of new line. Please check the [ASCII Table](http://www.asciitable.com/). – Uma Kanth Jun 11 '15 at 13:02
  • David and Pshemo did the right search, they gave you the right question [here](http://stackoverflow.com/questions/30687153/use-int-as-input-for-gregoriancalendar-but-am-not-able-to-continue-scanning-java). – Uma Kanth Jun 11 '15 at 13:05
  • @psychocoder you cannot read integer values using `System.in.read()`, you will need some extra help(`Scanner` or `BufferedReader`). Every character is considered an input using `System.in.read()`. – Uma Kanth Jun 11 '15 at 13:14
  • Yeah. I got it. Thank you. and I will use Scanner instead. – Max410 Jun 11 '15 at 13:15
1

System.in.read() doesn't read a number, it reads one byte and returns its value as an int.

If you enter a digit, you get back 48 + that digit because the digits 0 through 9 have the values 48 through 57 in the ASCII encoding.

To read a number from System.in you should use a Scanner.

Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30
0

use Scanner class instead:

 import java.util.Scanner;
 public class Adddemo {

    public static void main(String[] args) throws IOException {
        Scanner read=new Scanner(System.in);
        int i,j,k;
        System.out.println("enter value of i: ");
        i=(int)read.nextInt();
        System.out.println("enter value of j: ");
        j=(int)read.nextInt();

        k=i+1;

        System.out.println("sum is: "+k);
    }    
 }
nafas
  • 5,283
  • 3
  • 29
  • 57
  • Yeah, `InputStream.read()` only reads a single byte at a time. If your console is using a multi-byte character encoding (such as UTF-16) then it won't work. – Phylogenesis Jun 11 '15 at 12:52
  • I want to read the second value. It doesn't even ask for that. And i know Scanner class. I have to use System.in.read(). – Max410 Jun 11 '15 at 13:03
  • @psychocoder `System.in.read()` reads only a single byte. It sounds like you're pressing enter after typing the first number (you shouldn't). – Phylogenesis Jun 11 '15 at 13:08
  • Yeah. I got it. Thank you. – Max410 Jun 11 '15 at 13:16
0

System.in.read() reads 1 byte at a time.

if you want to feed your input values for i and j , do this
Leave one space between 1 and 2 while giving input in console
1 will be taken as value for i
2 will be taken as value for j

giving input as 12 (no spaces) will also yield the same result, cuz each byte is considered as an input

program
    int i,j;
        char c,d;
        System.out.println("enter value of i: ");
        i=(int)System.in.read();
        System.out.println("enter value of j: ");
        j=(int)System.in.read();

        System.out.println("i is: "+i);
        System.out.println("j is: "+j);

Output:
enter value of i,j: 
1 2          //leave one space 

i is: 1
j is: 2

let me know if you didn't understand yet

divine
  • 4,746
  • 3
  • 27
  • 38