I started learning java a few days ago. I prefer C++ but since I'll need java this college semester I decided to learn a few things to ease myself in. Anyway in C++ I always used a foolproof input method for taking user input of primitive numeric types. I picked that method up from here.
So I decided to somewhat transfer that to Java with perhaps an added functionality or two. However it turned out to be quite a bit messier than what I thought.
This is my current code:
FoolproofInput.java
package mrplow.input;
import java.util.Scanner;
/*
----------------------------------------------------------------------
@by MrPlow
Class designed for bullet-proofing user input for Integer, Float and
Double primitive types
They check if the next item in the input stream is valid:
- if it is, the read value is returned
- if it is not, the prompt loops until the correct item is input
All of the methods are used in the same way:
ex.
myInt = FoolproofInput.readInt("Give me an int");
There are also read<T>Rng methods
They are used for range input and contain two additional parameters
which are the lower range bound "lowest" and upper range bound "highest"
ex.
myInt = FoolproofInput.readIntRng("Give me an int between 10 - 20", 11,19);
------------------------------------------------------------------------
*/
public class FoolproofInput
{
static private Scanner scan = new Scanner(System.in);
static private boolean isValid = false;
static private int intValue = 0;
static private float floatValue = 0;
static private double doubleValue = 0;
static public int readInt(String prompt)
{
intValue = 0;
do
{
System.out.print(prompt);
if(scan.hasNextInt())
{
intValue = scan.nextInt();
isValid = true;
scan.nextLine();
}
else
{
System.out.println("Invalid input! Requested an Int, recieved something else.");
isValid = false;
scan.nextLine();
}
}while(!isValid );
return intValue;
}
static public float readFloat(String prompt)
{
floatValue = 0;
do
{
System.out.print(prompt);
if(scan.hasNextFloat())
{
floatValue = scan.nextFloat();
isValid = true;
scan.nextLine();
}
else
{
System.out.println("Invalid input! Requested a Float, recieved something else.");
isValid = false;
scan.nextLine();
}
}while(!isValid );
return floatValue;
}
static public double readDouble(String prompt)
{
doubleValue = 0;
do
{
System.out.print(prompt);
if(scan.hasNextDouble())
{
doubleValue = scan.nextDouble();
isValid = true;
scan.nextLine();
}
else
{
System.out.println("Invalid input! Requested a Double, recieved something else.");
isValid = false;
scan.nextLine();
}
}while(!isValid );
return doubleValue;
}
static public int readIntRng(String prompt, int lowest, int highest)
{
int intValue = 0;
do
{
System.out.print(prompt);
if(!scan.hasNextInt())
{
System.out.println("Invalid input! Requested an Int, recieved something else.");
isValid = false;
scan.nextLine();
}
else
{
intValue = scan.nextInt();
if( intValue >= lowest && intValue <= highest )
{
isValid = true;
scan.nextLine();
}
else
{
System.out.println( "Invalid input! The input number is not in range:"
+ "(" + lowest + "-" + highest + ")." );
intValue = 0;
}
}
}while(!isValid );
return intValue;
}
static public float readFloatRng( String prompt, float lowest, float highest )
{
float floatValue = 0;
do
{
System.out.print(prompt);
if(!scan.hasNextFloat())
{
System.out.println("Invalid input! Requested a Float, recieved something else.");
isValid = false;
scan.nextLine();
}
else
{
floatValue = scan.nextFloat();
if(floatValue >= lowest && floatValue <= highest)
{
isValid = true;
scan.nextLine();
}
else
{
System.out.println( "Invalid input! The input number is not in range:"
+ "(" + lowest + "-" + highest + ")." );
floatValue = 0;
}
}
}while(!isValid );
return floatValue;
}
static public double readDoubleRng( String prompt, double lowest, double highest )
{
double doubleValue = 0;
do
{
System.out.print(prompt);
if(!scan.hasNextDouble())
{
System.out.println("Invalid input! Requested a Double, recieved something else.");
isValid = false;
scan.nextLine();
}
else
{
doubleValue = scan.nextDouble();
if(doubleValue >= lowest && doubleValue <= highest)
{
isValid = true;
scan.nextLine();
}
else
{
System.out.println( "Invalid input! The input number is not in range:"
+ "(" + lowest + "-" + highest + ")." );
doubleValue = 0;
}
}
}while(!isValid );
return doubleValue;
}
}
Basically this has too much repeat code and the only differences are the (hasNext() methods).
So is there any way this code can be shortened? And are there any possible bugs that I've missed?
P.S: Also why doesn't this place have a "user-input" or even "input" tag?