Discussion:
Newbie seeking comments on StdinReader class
(too old to reply)
a***@yahoo.com
2011-09-05 14:00:39 UTC
Permalink
Greetings,

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;
}
}
markspace
2011-09-05 15:42:22 UTC
Permalink
Post by a***@yahoo.com
Greetings,
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.
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
{
}
langrenfengzi
2011-09-10 13:22:49 UTC
Permalink
Post by a***@yahoo.com
I want the class to silently block until the user enters the
correct input.
Here is my solution.It's a method named readInt, other methods are simil=
ar.


import java.util.Scanner;

public class StdinReader{

public static void main(String[] args){

Scanner scanner =3D new Scanner(System.in);
System.out.println(readInt(scanner));
}

public static int readInt(Scanner scanner){

int digit =3D 0;
while(true){
try{
digit =3D Integer.parseInt(scanner.next());
break;
}catch(Exception e){

}
}
return digit;
}
}

--- Posted via news://freenews.netfront.net/ - Complaints to ***@netfront.net ---
Roedy Green
2011-09-11 03:12:05 UTC
Permalink
Post by a***@yahoo.com
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)
Loading...