twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Problem with input from keyboard
|
Posted: May 6, 2004 5:41 AM
|
|
> The program worked although it took about 5 seconds before > I could start entering text. I would have preferred to > avoid using javax though & to know why the previous codes > did not work. I've never used System.in the way that you are, so I'm not sure what the problem is. On the rare occasions that I get console input, I use a class that is specifically designed for it. Here is the readString() method out of that class. I did not write the class, but it was posted as open source, so it should be OK to use a fragment.
public static String readString()
{
int ch = 0;
String r = "";
boolean done = false;
while (!done)
{
try
{
ch = System.in.read();
if (ch < 0 || (char)ch == '\n')
done = true;
else if ( (char) ch != '\r')
r = r + (char) ch;
}
catch(java.io.IOException e)
{
done = true;
return null;
}
}
lastChar = ch;
return r;
}
|
|