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?
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');
}
};