Read from keyboard(Console application)

S

serge

Hi everyone,
I have been trying to read a string of characters from the keyboard but I
keep getting an IOException. I am attaching the code. Any help would be very
appreciated thanks.

/*
This class is to be used as new readline method
*/
import java.io.*;
class ReadInput
{
public static void main(String[] args)
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);

ReadInput r = new ReadInput();
try{
r.readline(in);
}
catch (IOException e)
{
System.out.print("\n Exception caught \n");
}

}

public void readline(BufferedReader b)throws IOException
{
String input ="";
//Buffer is empty at the moment
while(true)
{
System.out.print("Echo<");
input = b.readLine();
// else
{
if ("".equals(input))
{
System.out.println("End");
break;
}
else
{
if("OK".equals(input))
{
System.out.println("OK command received ...");
}
}
System.out.println("Echo>" + input.toUpperCase());
}
}
//in.close();

}
}
 
P

Paul

Hi everyone,
I have been trying to read a string of characters from the keyboard but I
keep getting an IOException. I am attaching the code. Any help would be
very appreciated thanks.
...

Works fine here though..(?)

Paul
 
P

Paul

Your code works for me.
Note that BufferedReader's readLine() will return null if the end of the
stream has been reached, so you should check for that.
What IOException are you getting?
where you have
System.out.print("\n Exception caught \n");
you might also add
e.printStackTrace();
so you can evaluate the error.
--Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top