I wanted to make an input which accepts numbers from 1 - 10 and prints the range.
I need to check if the input is an integer (check), check if the range is 0-10 (check), and if it's not any of those things, to ask the user again. So, a recursive method?
Currently I have this:
import java.util.Scanner; import java.util.InputMismatchException; public class FinalTest { public static void main (String [] args) { Scanner in = new Scanner(System.in); int k = 0; System.out.print("int - "); try { k = in.nextInt(); } catch (InputMismatchException e) { System.out.println("ERR: Input"); System.exit(1); } if(k <= 10 && k > 0) { for(int j=1; j <= k; j++) { System.out.println(j); } } else { System.out.println("ERR: Oob"); System.exit(1); } } } I would like to replace the "System.exit()" so that it re attempts to ask the user for input again.
calling main(); produces an error.
How do I correctly call the main method in this case?
whileanddo-whileStatementswhile(don't have valid answer){ask for answer}