String UNP; Scanner in = new Scanner(System.in); UNP = in.next(); if(Integer.parseInt(UNP) > 1 && Integer.parseInt(UNP) <= 21) out.println("\n Объект выбран\n"); else out.println("\n Ошибка выбора объекта\n"); When you enter a string consisting not only of numbers, an error occurs.
Java.lang.NumberFormatException: For input string: "qq" at java.lang.NumberFormatException.forInputString (NumberFormatException.java:65) at java.lang.Integer.parseInt (Integer.java low80) at java.lang.Integer.parseInt (Integer.java:615) at Levchenko.App.CLIENT_OBJECT.INPUT_UNP (CLIENT_OBJECT.java:44) at Levchenko.App.Main.main (Main.java:8) ------ -------------------------------------------------- ---------------- BUILD FAILURE -------------------------------- ---------------------------------------- Total time: 2.986s Finished at: Fri Mar 03 22:58:41 MSK 2017 Final Memory: 6M / 106M -------------------------------------- ------------------------------------ Failed to execute goal org.codehaus.mojo: exec-maven- plugin: 1.2.1: exec (default-cli) on project App: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
Re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.
Please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
How can I check the possibility of converting string to int for comparison before converting string to int and checking that the numbers range matches?
do { UNP = in.next(); if(UNP.matches("\\d+")){ if(Integer.parseInt(UNP) > 1 && Integer.parseInt(UNP) <= 21) out.println(" Объект выбран\n"); else out.println(" Ошибка выбора объекта\n"); } else out.println(" Ошибка выбора объекта\n"); } while(Integer.parseInt(UNP) < 1 || Integer.parseInt(UNP) > 21); If a string is entered not only with numbers, then the condition fulfills 1 time, displays the same error and exits do while.
Corrected assignment before checking while.