Extra value in input stream when running code outside of Intellij

91 Views Asked by At

So I am trying to see how streams work in java and I wrote some code that is supposed to take 2 one digits integers and return their sum.

Here is the working code in intellij:

public class practice {
public static void main(String[] args) throws IOException {
    int x = Character.getNumericValue((char)System.in.read());
    System.in.read(); // reads the -1 so that input stream is now empty
    int y = Character.getNumericValue((char)System.in.read());

    System.out.println(x+y);

}}

And here is the working code outside of intellij, in eclipse or whatever else:

public class practice {
public static void main(String[] args) throws IOException {
    int x = Character.getNumericValue((char)System.in.read());
    System.in.read();
    System.in.read();
    int y = Character.getNumericValue((char)System.in.read());

    System.out.println(x+y);

}}

I have had to add an extra System.in.read() as somehow there is another value in the stream, in order to check this i printed that value to console and got it's value as 13, which I linked with a "carriage return" from an ASCII table, what is a carriage return, and why is this not occurring in intellij?

Thanks for any help.

0

There are 0 best solutions below