• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java input failure?

Schmide

Diamond Member
I was helping a friend with his assignment. This wasn't the initial problem that was removed to protect the innocent. I ran into this problem when you mix input sources.

This is a very simplified version of it. Basically (line 26 and 31) if System.in.read() is used the next time cBufferedReader.readLine() is executed it will pass over it.

Any ideas why this happens and is there a way around it?

Code:
/*
LIBRARIES
 */
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.lang.*;

/**
 *
 * @author schmide
 */

public class BuffProb {

   public static void main(String args[]) {

        int iLoopResponse;
        Scanner cScan = new Scanner(System.in);
        InputStreamReader cInput = new InputStreamReader(System.in);
        BufferedReader cBufferedReader = new BufferedReader(cInput);
        do {
            String sInput = "";
            System.out.println("Enter Sting");
            try {
                sInput = cBufferedReader.readLine();
            } catch (Exception e) {
                System.out.println("Error ");
            }

            // This is where the error is caused
            // If System.in.read() is used
            // the next time cBufferedReader.readLine()
            // is executed it will pass over it
            iLoopResponse='n';
            System.out.println("Loop? Y/N");
            if(true) // chage to see error here
            {
                // works
                try {
                    sInput = cBufferedReader.readLine();
                } catch (Exception e) {
                    System.out.println("Error Reading");
                }
                iLoopResponse=sInput.charAt(0);
            } else {
                // casues failure
                try {
                    iLoopResponse = System.in.read();
                } catch (Exception e) {
                    System.out.println("Error Reading");
                }
            }
        } while (iLoopResponse=='Y' || iLoopResponse=='y');
    }
};
 
When you create your buffered input reader you are tying it to System.in. Then when you read the value manually using System.in.read() the buffered reader will never see the input because you read the value already.

Why would you try to read from System.in after attaching it to a buffered reader? The whole point of using the buffered reader is so that you don't have to parse the raw input stream and look for line breaks etc, instead you get to process input as lines of text as they become available.
 
Because buffered reader doesn't support a single char input?

Ironically it does support a single int input.

If you want to be able to respond to individual key presses as they occur in realtime then don't use the buffered reader. The whole point of the buffered reader is to buffer the input until you get to a newline. If that won't work for your problem then simply don't use it.

There's nothing stopping you from constructing a loop like

char c = System.in.read();
while (c != 'q') {
c = System.in.read();
//handle input
}
 
Back
Top