Java Reference
In-Depth Information
InputFlush();
printPrompt(prompt);
try
{
aChar = System.in.read();
}
catch(java.io.IOException e)
{
System.out.println("Input error");
}
inputFlush();
return (char) aChar;
}
Sincethereisno“rawmode”consoleinputinJava,themethodtoread
a single character waits until the user presses the key that terminates in-
put, usually the one labeled <Enter> or <Return>. In fact, inChar() re-
turns the first character typed but it cannot prevent the user from typing
more than one character. For this reason the method calls inputFlush()
before exiting. Also note that the input, which is of type int, is typecast
intoatypecharinthereturnstatement.
Capturing an input string requires a bit more processing. The method
inString,listedhere,performstheprocessing.
public static String inString()
{
int aChar;
Strings="";
boolean finished = false;
while(!finished)
{
try
{
aChar = System.in.read();
if (aChar<0||(char)aChar == '\n')
finished = true;
else if ((char)aChar != '\r')
s=s+(char) aChar;
// Append to string
}
catch(java.io.IOException e)
{
System.out.println("Input error");
finished = true;
}
}
return s;
}
Search WWH ::




Custom Search