2

Here's the code:

    package testpack;
    import java.io.*;

    public class InputStreamReadDemo {


        private void readByte() throws IOException {
            System.out.print("Enter the byte of data that you want to be read: ");        
            int a = System.in.read();
            System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
            // tried writing System.in.close(); and System.in.reset();
        }
        private void readLine() throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter a line of text: ");
            String res = br.readLine();
            System.out.println("You entered the following line of text: "+res);
            // ... and tried writing br.close();
            // but that messes things up since the input stream gets closed and reset..
        }

        public static void main(String[] args) throws IOException {
            InputStreamReadDemo isrd = new InputStreamReadDemo();
            isrd.readByte();                                                // method 1
            isrd.readLine();                                                // method 2
        }
    }

Here's the output when I run this (and I input "Something" when the first method is called, the second method then auto-reads "omething" for some reason instead of letting me input again..):

run:
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: You entered the following line of text: omething
BUILD SUCCESSFUL (total time: 9 seconds)
Petrus K.
  • 840
  • 7
  • 27
  • 56

1 Answers1

5

The reason it prints out "omething" is that in your call to readByte, you only consume the first byte of your input. Then readLine consumes the remainder of the bytes in the stream.

Try this line at the end of your readByte method: System.in.skip(System.in.available());

That will skip over the remaining bytes in your stream ("omething") while still leaving your stream open to consume your next input for readLine.

Its generally not a good idea to close System.in or System.out.

import java.io.*;

public class InputStreamReadDemo {


    private void readByte() throws IOException {
        System.out.print("Enter the byte of data that you want to be read: ");        
        int a = System.in.read();
        System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
        System.in.skip(System.in.available());
    }
    private void readLine() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a line of text: ");
        String res = br.readLine();
        System.out.println("You entered the following line of text: "+res);
        // ... and tried writing br.close();
        // but that messes things up since the input stream gets closed and reset..
    }

    public static void main(String[] args) throws IOException {
        InputStreamReadDemo isrd = new InputStreamReadDemo();
        isrd.readByte();                                                // method 1
        isrd.readLine();                                                // method 2
    }
}

Results in:

$ java InputStreamReadDemo
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: Another thing
You entered the following line of text: Another thing

UPDATE: As we discussed in chat, it works from the command line, but not in netbeans. Must be something with the IDE.

Mjonir74
  • 261
  • 3
  • 9
  • Sorry it didn't work for you. I just copied your posted code, put the line I suggested at the end of readByte, compiled and ran the application. It works for me. – Mjonir74 Oct 06 '11 at 22:22
  • that's really weird.. this is what I'm running: http://pastebin.com/5P5pKYxs it IS the same code as above, isn't it.. – Petrus K. Oct 06 '11 at 22:32
  • Yes, I pasted from your link and recompiled and ran it again to make sure. It still works for me. I am using java version "1.6.0_26" from sun/oracle. This may sound like a patronizing question, but did you remember to recompile after making your code changes? – Mjonir74 Oct 06 '11 at 22:40
  • Haha, don't worry I'm not offended, it's late at night here so it's plausible that I'd make such a mistake, but I didn't. So to answer your question, yes I did recompile, I even cleaned and rebuilt the whole project. Maybe I should check what version I'm running, what's the fastest way of doing that? – Petrus K. Oct 06 '11 at 22:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4086/discussion-between-ryuji89-and-mjonir74) – Petrus K. Oct 06 '11 at 22:58
  • After compiling the sourcecode from the commandline and running it with java.exe the issue got resolved. The IDE I was using is Netbeans 6.9.1 and Java version 1.6.0.24 – Petrus K. Oct 06 '11 at 23:06