import java.util.Scanner;

public class Validator
{
     public static String getString(Scanner sc, String prompt)
     {
          System.out.println(prompt);
          String s = sc.next();   // read the user entry
          sc.nextLine();  // discard any other data entered on the line
          return s;
     }
     public static int getInt(Scanner sc, String prompt)
     {
          int i = 0;
          boolean isValid = false;
          while (isValid == false)
          {
              System.out.print(prompt);
              if (sc.hasNextInt())
              {
                   i = sc.nextInt();
                   isValid = true;
              }
              else
              {
                   System.out.println("Error! Invalid integer value. Try again");

              }
              sc.nextLine();    // discard any other data entered on the line
          }
          return i;

     }
     public static int getInt(Scanner sc, String prompt, int min, int max)
     {
          int i = 0;
          boolean isValid = false;
          while (isValid == false)
          {
               i = getInt(sc, prompt);
               if (i <= min)
                   System.out.println("Error! Number must be greater than " + min + ".");
               else if (i >= max)
                   System.out.println("Error! Number must be lesser than " + max + ".");
               else
                   isValid = true;

          }
          return i;
     }
     public static double getDouble(Scanner sc, String prompt)
     {
          double d = 0;
          boolean isValid = false;
          while (isValid == false)
          {
              System.out.print(prompt);
              if (sc.hasNextDouble())
              {
                   d = sc.nextDouble();
                   isValid = true;
              }  
              else
              {
                   System.out.println("Error! Invalid decimal value. Try again");
              } 
              sc.nextLine();
          }
          return d;
     }   
     public static double getDouble(Scanner sc, String prompt, double min, double max)
     {
           double d = 0;
           boolean isValid = false;
           while (isValid == false)
           {
                d = getDouble(sc, prompt);
                if (d <= min)
                    System.out.println("Error! Number must be greater than " + min + ".");
                else if (d >= max)
                    System.out.println("Error! Number must be lesser than " + max + ".");
                else
                    isValid = true;
           }
           return d;
     }
}

// This class is part of the presentation layer for 
//a console application. It can be called from the
//application's main method. page 216.









Site hosted by Angelfire.com: Build your free website today!