Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie seeking comments on StdinReader class

12 views
Skip to first unread message

anirud...@yahoo.com

unread,
Sep 5, 2011, 10:00:39 AM9/5/11
to
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

unread,
Sep 5, 2011, 11:42:22 AM9/5/11
to
On 9/5/2011 7:00 AM, anirud...@yahoo.com wrote:
> 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

unread,
Sep 10, 2011, 9:22:49 AM9/10/11
to
在 Mon, 05 Sep 2011 22:00:39 +0800,<anirud...@yahoo.com> 写道:

The key sentence:
> 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 similar.


import java.util.Scanner;

public class StdinReader{

public static void main(String[] args){

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

public static int readInt(Scanner scanner){

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

}
}
return digit;
}
}

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---

Roedy Green

unread,
Sep 10, 2011, 11:12:05 PM9/10/11
to
On Mon, 5 Sep 2011 07:00:39 -0700 (PDT), anirud...@yahoo.com wrote,
quoted or indirectly quoted someone who said :

>
>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)

0 new messages