a***@yahoo.com
2011-09-05 14:00:39 UTC
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;
}
}
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;
}
}