I am a Java newbie and I have created a class which reads input from
Stdin. I want the class to silently block until the user enters the
correct input. Here's what I have developed. I would appreciate
expert comments on this, they way a seasoned professional would
approach this problem. (Yes, I do realize that Java considers command
line input old fashioned).
Thanks,
Anirudh
//////////////////////////////////////////
import java.io.*;
import java.util.Scanner;
public class StdinReader {
/*
Method to read integer from standard input
*/
public static int readInt(){
int intVal = 0;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextInt())
{
intVal = in.nextInt();
break;
}
else dummyStr = in.next();
}
return intVal;
}
/*
Method to read short from standard input
*/
public static short readShort(){
short shortVal =0;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextShort())
{
shortVal = in.nextShort();
break;
}
else dummyStr = in.next();
}
return shortVal;
}
/*
Method to read long from standard input
*/
public static long readLong(){
long longVal =0;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextLong())
{
longVal = in.nextLong();
break;
}
else dummyStr = in.next();
}
return longVal;
}
/*
Method to read byte from standard input
*/
public static byte readByte(){
byte byteVal =0;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextByte())
{
byteVal = in.nextByte();
break;
}
else dummyStr = in.next();
}
return byteVal;
}
/*
Method to read float from standard input
*/
public static float readFloat(){
float floatVal =0;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextFloat())
{
floatVal = in.nextFloat();
break;
}
else dummyStr = in.next();
}
return floatVal;
}
/*
Method to read double from standard input
*/
public static double readDouble(){
double doubleVal =0;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextDouble())
{
doubleVal = in.nextDouble();
break;
}
else dummyStr = in.next();
}
return doubleVal;
}
/*
Method to read boolean from standard input
*/
public static boolean readBoolean(){
boolean boolVal = false;
String dummyStr = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNextBoolean())
{
boolVal = in.nextBoolean();
break;
}
else dummyStr = in.next();
}
return boolVal;
}
/*
Method to read String from standard input
*/
public static String readString(){
String strVal = "";
Scanner in = new Scanner(System.in);
for(;;) {
if (in.hasNext())
{
strVal = in.next();
break;
}
}
return strVal;
}
}
This is what I came up with. Whether it's really better or not I think
depends on your requirements. Lightly tested.
public static int test3( InputStream ins )
throws MyEndOfInputException
{
Scanner scanner = new Scanner( ins );
while( scanner.hasNext() ) {
if( scanner.hasNextInt() )
return scanner.nextInt();
else
scanner.next();
}
throw new MyEndOfInputException();
}
}
class MyUtilsException extends Exception
{
}
class MyEndOfInputException extends MyUtilsException
{
}
>
>I am a Java newbie and I have created a class which reads input from
>Stdin. I want the class to silently block until the user enters the
>correct input. Here's what I have developed. I would appreciate
>expert comments on this, they way a seasoned professional would
>approach this problem. (Yes, I do realize that Java considers command
>line input old fashioned).
stdin blocks all by itself. You can do this more simply with
readLine. See http://mindprod.com/applet/fileio.html
to generate you a program to do it.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)