Is the call to getNum(in) in retry(in) a recursive call??
What I'm trying to do is keep requesting input when NumberFormatException
is thrown.
import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; public class test { private static int retry(BufferedReader in) throws IOException { System.out.print("Enter number: "); return test.getNum(in); } public static int getNum(BufferedReader in) throws IOException { int num; try { num = Integer.parseInt(in.readLine()); } catch(NumberFormatException e) { System.out.println("INVALID INPUT!!!\n"); num = retry(in); } return num; } public static void main(String[] args) { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String name; int num1, num2; try { System.out.print("Enter a line: "); name = input.readLine(); System.out.println("Hello : " + name); System.out.print("Enter first number: "); num1 = test.getNum(input); System.out.print("Enter second number: "); num2 = test.getNum(input); System.out.println(name + " the answer is " + (num1 + num2)); } catch(IOException e) { System.out.println("IO ERROR!!!!\n" + e.getMessage() + "\n"); } } }