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.

    1 answer 1

    You can use exception handling:

     String UNP; Scanner in = new Scanner(System.in); UNP = in.next(); try{ if(Integer.parseInt(UNP) > 1 && Integer.parseInt(UNP) <= 21) System.out.println("\n Объект выбран\n"); else System.out.println("\n Ошибка выбора объекта\n"); } catch(NumberFormatException e){ System.out.println("\n Неверный формат\n"); } 

    You can add a function that also deals with exception handling. It turns out like this:

     public static void main (String[] args){ String UNP; Scanner in = new Scanner(System.in); UNP = in.next(); if(isNumber(UNP)){ if(Integer.parseInt(UNP) > 1 && Integer.parseInt(UNP) <= 21) System.out.println("\n Объект выбран\n"); else System.out.println("\n Ошибка выбора объекта\n"); } else System.out.println("\n Неверный формат\n"); } public static boolean isNumber(String s) { try { Integer.parseInt(s); return true; } catch (NumberFormatException e) { return false; } } 

    You can also check whether a string is an integer (non-negative) number using regular expressions:

     if(UNP.matches("\\d+")){ if(Integer.parseInt(UNP) > 1 && Integer.parseInt(UNP) <= 21) System.out.println("\n Объект выбран\n"); else System.out.println("\n Ошибка выбора объекта\n"); } else System.out.println("\n Неверный формат\n"); 

    For any integers, the expression will be:

     UNP.matches("-?\\d+") 
    • And if the operator can check? In one line. - aaa
    • You can, added to the answer. Handling exceptions is much easier than looking for other ways to check a string, for example, with regular expressions. - Kirill Malyshev
    • Perhaps without a function? In one line. - aaa
    • Added a way with regular expressions. - Kirill Malyshev
    • one
      digit is a digit , number is a number . Accordingly, the method should be called isNumber . - Regent