I have written a Java class called "Helper" to simplify I/O for beginner Java programmers. I've tested it on some simple input and it seems to work alright. Could anyone offer a subtle, constructive critique of my use of the Scanner class?
Code:// This class simplifies input and output for beginners
import java.util.*;
public class Helper {
private static Scanner s1;
//Scanner methods
public static double getDouble() {
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
double x = s1.nextDouble();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter double:");
}
}
System.out.println("I give up! I'm entering 0.0 FOR you.");
return 0;
}
public static int getInt(){
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
int x = s1.nextInt();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter int:");
}
}
System.out.println("I give up! I'm entering 0 FOR you.");
return 0;
}
public static long getLong(){
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
long x = s1.nextLong();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter long:");
}
}
System.out.println("I give up! I'm entering 0 FOR you.");
return 0;
}
public static float getFloat(){
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
float x = s1.nextFloat();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter float:");
}
}
System.out.println("I give up! I'm entering 0.0 FOR you.");
return 0f;
}
public static String getString() {
s1 = new Scanner(System.in);
String s = s1.nextLine();
return s;
}
public static double getDouble(String msg){
System.out.print(msg + " ");
return getDouble();
}
public static int getInt(String msg){
System.out.print(msg + " ");
return getInt();
}
public static long getLong(String msg){
System.out.print(msg + " ");
return getLong();
}
public static float getFloat(String msg){
System.out.print(msg + " ");
return getFloat();
}
public static String getString(String msg){
System.out.print(msg + " ");
return getString();
}
//Print methods
public static void print(String msg){
System.out.println(msg);
}
public static void print(double d){
System.out.println(d);
}
public static void print(long n){
System.out.println(n);
}
}