Complete-er answer at the bottom
Haven't run it at all to check, though it seems like it could have something to do with the use of a do-while loop instead of a while loop
This format will make sure that the character 'q' is not read before it attempts to output anything
while(ch!='q')
{
System.out.println(ch);
ch = (char)br.read();
}
And this format prints the read character before testing if it is valid
do {
ch = (char)br.read();
System.out.println(ch);
} while(ch!='q');
EDIT after some trial/error
With this version, I have used the Scanner class which is quite similar, and I am more familiar with it. It may go something like so:
- Create Object(s) for reading the data
BufferedReader,Scanner,etc.
- Check if there is content present
- Accept the data as a
String and read the first character
- Output if the
String is is not q
- Cycle the test-> read -> print -> loop, till
q is entered
Scanner input = new Scanner(System.in);
System.out.println("Enter characters, 'q' to quit.");
String read = input.nextLine();
while(read.length() <= 1 && read.charAt(0) != 'q')
{
System.out.print(read);
read = input.nextLine();
}
The (almost) original method*
Eureaka!
- name.read() Reads a single character --
However, this returns an
int datatype which cannot be converted with the (char) mask.
- name.readLine() Reads a line of text -- With this, you can simply take the character at index 0
https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html
do {
full = br.readLine();
System.out.print(full);
} while(full.length() <= 1) && full.charAt(0) != 'q');
Not sure what the best way to do it would be between a do-while and a while loop, and that may ultimately come down to use case and opinion. and you may want to make a boolean method too
// true if String matches 'q' or "quit"
public static boolean isQuit(input)
{
String lowercase = input.toLower();
char start = input.charAt(0);
return (lowercase.equals("quit")
|| (input.length() <= 1 && start == 'q'));
}