Critiques? Criticisms?
/**
* A method that prompts for a specified input and checks to see if a input is valid
* it will contine to ask until the correct input has been entered. I have not figured out how to crash it yet.
*
* @author Andrew Hood
* @version 3/02/2012
*
*/import java.util.*;
import java.io.*;
public class errorCheck
{
static int getValueExpectInt()
{
String _temp = "";
boolean correctInput;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do
{
try
{
_temp = br.readLine();
correctInput = Integer.valueOf(_temp).getClass().getName().equals("java.lang.Integer");
}
catch(Exception ex)
{
System.out.print("That's not an integer. Try again: ");
correctInput = false;
}
} while(correctInput == false);
return Integer.valueOf(_temp);
}
static double getValueExpectDouble()
{
String _temp = "";
boolean correctInput;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do
{
try
{
_temp = br.readLine();
correctInput = Double.valueOf(_temp).getClass().getName().equals("java.lang.Double");
}
catch(Exception ex)
{
System.out.print("That's not a Double. Try again: ");
correctInput = false;
}
} while(correctInput == false);
return Double.valueOf(_temp);
}
public static void main(String[] args)
{
int myInt;
double myDouble;
System.out.print("Please enter an integer: ");
myInt = getValueExpectInt();
System.out.print("Pleas enter a double: ");
myDouble = getValueExpectDouble();
System.out.println("Your int: " + myInt + " Your double: " + myDouble);
}
}

New Topic/Question
Reply



MultiQuote





|