How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

11,729

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.

Share:
11,729
Petrus K.
Author by

Petrus K.

Graduate of Malmö University in Telecommunications Engineering (B.Sc.) Links: - LinkedIn profile - GitHub repo

Updated on June 04, 2022

Comments

  • Petrus K.
    Petrus K. almost 2 years

    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)
    
  • Mjonir74
    Mjonir74 over 12 years
    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.
  • Petrus K.
    Petrus K. over 12 years
    that's really weird.. this is what I'm running: pastebin.com/5P5pKYxs it IS the same code as above, isn't it..
  • Mjonir74
    Mjonir74 over 12 years
    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?
  • Petrus K.
    Petrus K. over 12 years
    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.
    Petrus K. over 12 years
  • Petrus K.
    Petrus K. over 12 years
    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