/** *Class for simplified console input of all variable types ..... A short description : Getting console input in java is a very tedious process ! This java class will help you to input all types of variables from console with relatively ease. All nine fundamental variable types are covered : byte, short, int, long, float, double, char, String, and, boolean . It ignores appropiate input errors smartly ( if ecc is set to true, the default is false ). Copy this file to your project/java directory and name it console.java . Compile it using command like C:\java> javac console.java - and you are all done. Please see the use of this class and input methods in tescuinput.java class example below, if you know little java - it is walk in the park for you ! I wish you enjoy my effort, let me know through e-mail !!! * * @author (Shirshasin Ghosh Of Naihati, West Bengal, India... shirsha@rediffmail.com) * @version ( 2.0 : 10-August : 9:10 P.M ) */ /* Begin of Class */ import java.io.BufferedReader ; import java.io.InputStreamReader ; public class cui { /* public variables for class */ public int i , j ; public String hold = "" ; public char a ; public boolean checkecc = false ; /* 1)Reads a number as string and returns Byte type */ public byte getByte ( String prompt ) { boolean validResponse = false; String str = ""; byte number = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); if (checkecc) str = convertwhole(str) ; number = Byte.parseByte(str); validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return number; } /* End of getByte Method */ /* 2)Reads a number as string and returns short type */ public short getShort(String prompt) { boolean validResponse = false; String str = ""; short number = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); if (checkecc) str = convertwhole(str) ; number = Short.parseShort(str); validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return number; } /* End of getShort Method */ /* 3)Reads a number as string and returns int type */ public int getInt(String prompt) { boolean validResponse = false; String str = ""; int number = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); if (checkecc) str = convertwhole(str) ; number = Integer.parseInt(str); validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return number; } /* End of getInt Method */ /* 4)Reads a number as string and returns long type */ public long getLong(String prompt) { boolean validResponse = false; String str = ""; long number = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); if (checkecc) str = convertwhole(str) ; number = Long.parseLong(str); validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return number; } /* End of getLong Method */ /* 5)Reads a number as string and returns float type */ public float getFloat(String prompt) { boolean validResponse = false; String str = ""; float number = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); if (checkecc) str = convertwhole(str) ; number = Float.parseFloat(str); validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return number; } /* End of getFloat Method */ /* 6)Reads a number as string and returns double type */ public double getDouble(String prompt) { boolean validResponse = false; String str = ""; double number = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); if (checkecc) str = convertwhole(str) ; number = Short.parseShort(str); validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return number; } /* End of getDouble Method */ /* 7)Reads a character and returns a char */ public char getChar(String prompt) { boolean validResponse = false; char nextChar = ' ' ; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { nextChar = (char)in.read(); validResponse = true; } /* End of try */ catch (Exception e) { System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return nextChar ; } /* End of getChar method */ /* 8)Reads a string and returns that string */ public String getString(String prompt) { boolean validResponse = false; String str = ""; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); ; validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ return str ; } /* End of getString method */ /* 9)Reads a string and returns boolean */ public boolean getBoolean(String prompt) { boolean validResponse = false; String str = ""; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!validResponse) { System.out.print(prompt) ; try { str = in.readLine(); ; validResponse = true; } /* End of try */ catch (Exception e) { if(str.equals("uninitializedValue")) str = ""; System.out.println("Invalid Entry : "+e+"\n"); continue ; } /* End of catch */ } /* End of While Loop */ if ( str.equalsIgnoreCase("y") || str.equalsIgnoreCase("t") || str.equalsIgnoreCase("yes") || str.equalsIgnoreCase("true") || str.equalsIgnoreCase("1") ) return true ; else return false ; } /* End of getBoolean Method */ /* 10. A ECC-validate and triming/removing function */ public String convertfrac (String str1) { hold = "0" ; a = ' '; str1 = str1.trim() ; i = str1.length() ; int count = 0 ; for ( j = 0 ; j < i ; j++ ) { a = str1.charAt(j) ; if ( a == '.' ) count++ ; if ( ( Character.isDigit(a) || ( a == '.') ) && ( count <= 1) ) hold = hold + a ; } return hold ; } /* End of convertfrac method */ /* 11. Another ECC-validate and triming/removing function */ public String convertwhole (String str1) { str1 = str1.trim() ; i = str1.length() ; hold = "0" ; a = ' '; for ( j = 0 ; j < i ; j++ ) { a = str1.charAt(j) ; if ( Character.isDigit(a) ) hold = hold + a ; else if ( a == '.' ) return hold ; } return hold ; } /* End of convertwhole method */ /* 12. A Handy method for testing prime numbers */ public boolean isprime( long b ) { long a, c ; byte k = 1 ; a = Math.round( Math.sqrt(b) ) ; for ( c = 2; c <= a ; c++ ) { if ((b % c) == 0 ) { k = 0 ; break ; } } if (k == 1) return true ; else return false ; } /* End of isprime method */ /* 13. A handy method for giving spaces on a line */ public void space ( int n) { int i ; for ( i = 1 ;i <= n ; i++) System.out.print(" "); } /* End of space method */ /* 14. Set the ecc code to true of false */ public void ecc( boolean set ) { checkecc = set ; } /* 15. Simple message display method */ public void show( String prompt ) { System.out.print(prompt) ; } /* 16. Simple blank line showing method */ public void showline( int n ) { for ( i = 1 ; i <= n ; i++) System.out.print("\n") ; } } /***** End of class *****/