java - Limit number of prompted arguments -
i want prompt user input 2 numbers represent size nxn of board. if user write less 1 or more 2 numbers, program don't allow him , instead, handle exception.
for example:
public class tester { public static void main(string[] args) { scanner userinput = new scanner(system.in); system.out.print("board size nxn: "); int width = userinput.nextint(); int height = userinput.nextint(); } }
how can achieve using try-catch block limiting user input 2 numbers?
public class tester {
public static void main(string[] args) { scanner userinput = new scanner(system.in); system.out.print("board size nxn: "); try { int width = userinput.nextint(); int height = userinput.nextint(); if (userinput.hasnext()) { throw new illegalargumentexception(); } } catch (illegalargumentexception e) { system.err.println("enter 2 numbers"); } }
}
Comments
Post a Comment