import java.io.*;
import java.text.*;

public class FileInput{
    static int numinst = 0;
    static BufferedReader in = null;
    public static final int MAX = 5;
    public FileInput(String filename) {
 numinst++;
 try {
     if (in == null)
     in= new BufferedReader(new FileReader(filename));
 }catch (IOException e) {
     System.out.println("Could not read file:" + filename);
     System.out.println(e.getMessage());
     e.printStackTrace();
     System.exit(1);
 }
    }

  public void pause()
 {
  System.out.println("(Press ENTER key to continue...)");
  String s = readString();
 }
  public int readInt()
 {
  int k;
  k = readInt("");
  return k;
 }

  public long readLong()
 {
  long k;
  k = readLong("");
  return k;
 }

  public double readDouble()
 {
  double k;
  k = readDouble("");
  return k;
 }
  public float readFloat()
 {
  float k;
  k = readFloat("");
  return k;
 }


  public String readString()
 {
  String k;
  k = readString("");
  return k;
 }
  public char readChar()
        {
                char k;
                k = readChar("");
                return k;
        }

  public int readInt(String prompt) {
    boolean done = false;
    String s;
    int i=0, tries=0;
    while (!done && (tries < MAX)) {
 tries++;
 if (prompt != "")
 {
      System.out.print(prompt+ ":  ");
 }
      System.out.flush(); 
      try {
        s = in.readLine();
 if (!(prompt.equals(""))) System.out.println(s);
        i = Integer.parseInt(s);
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 
         System.out.println("Invalid Entry");
      }  
    } 
    if (!done) System.out.println("#### Error - Too many retries, giving up!");
    return i;
  }
  public long readLong(String prompt) {
    boolean done = false;
    String s;
    long i=0;
    int tries = 0;
    while (!done && (tries < MAX)) {
 tries++;
 if (prompt != "")
        {
      System.out.print(prompt+ ":  ");
        }   
      System.out.flush(); 
      try {
        s = in.readLine();
 if (!(prompt.equals(""))) System.out.println(s);
        i =  Long.parseLong(s);
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 

         System.out.println("Invalid Entry");
      }  
    } 
    if (!done) System.out.println("#### Error - Too many retries, giving up!");
    return i;
  } 
  public double readDouble(String prompt) {
    boolean done = false;
    String s;
    double d=0;
    int tries = 0;
    while (!done && (tries < MAX)) {
 tries++;

 if (prompt != "")
        {
      System.out.print(prompt+ ":  ");
        }   
      System.out.flush(); 
      try {
        s = in.readLine();
 if (!(prompt.equals(""))) System.out.println(s);
        d = new Double(s).doubleValue();
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 
         System.out.println("Invalid Entry");
     }  
    } 
    if (!done) System.out.println("#### Error - Too many retries, giving up!");
    return d;
  } 
  public float readFloat(String prompt) {
    boolean done = false;
    String s;
    float f = 0;
    int tries = 0;
    while (!done && (tries < MAX)) {
 tries++;

 if (prompt != "")
        {
      System.out.print(prompt+ ":  ");
        }   
      System.out.flush(); 
      try {
        s = in.readLine();
 if (!(prompt.equals(""))) System.out.println(s);
        f = new Float(s).floatValue();
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 
         System.out.println("Invalid Entry");
     }  
    } 
    if (!done) System.out.println("#### Error - Too many retries, giving up!");
    return f;
  } 
  public char readChar(String prompt) {
    boolean done = false;
    String s;
    char c = ' ';
    int tries = 0;
    while (!done && (tries < MAX)) {
 tries++;
 if (prompt != "")
        {
      System.out.print(prompt+ ":  ");
        }   
      System.out.flush(); 
      try {
        s = in.readLine();
 if (!(prompt.equals(""))) System.out.println(s);
        if (s.length() >0)
          c =  s.charAt(0);
        done = true; 
      }catch (IOException e){
        done = true;
      }
    } 
    if (!done) System.out.println("#### Error - Too many retries, giving up!");
    return c;
  } 
  public String readString(String prompt) {
    boolean done = false;
    String s = "";
    int tries = 0;
    while (!done && (tries < MAX)) {
 tries++;
 if (prompt != "")
        {
      System.out.print(prompt+ ":  ");
        }   
      System.out.flush(); 
      try {
        s = in.readLine();
 if (!(prompt.equals(""))) System.out.println(s);
        done = true; 
      }catch (IOException e){
        done = true;
      }
    } 
    if (!done) System.out.println("#### Error - Too many retries, giving up!");
    return s;
  } 

  public static void main(String [] args) {
    FileInput ui = new FileInput("test");
    System.out.println(ui.readString("Enter a string "));
    System.out.println(ui.readInt("Enter an integer "));
    System.out.println(ui.readLong("Enter a long without an L suffix "));
    System.out.println(ui.readChar("Enter a single character"));
    float f = ui.readFloat("Enter a float ");
    System.out.println(f);
    ui.pause();
    double d = ui.readDouble("Enter a double ");
    System.out.println(d);
  }

} 

