Just let users enter positive integers (without decimals or strings)?

I know how to ask the user to enter positive integers, but I don’t know how to approach the code to avoid an input error such as decimal or string input.

int seedValue; double angle, gunpowder; System.out.println("Please enter a positive integer seed value: "); seedValue = input.nextInt(); while (seedValue <= 0) { System.out.println("Please enter a positive integer seed value: "); seedValue = input.nextInt(); } System.out.println("That target is " + threeDec.format(gen.nextDouble() * 1000) + "m away."); 
+6
source share
5 answers

This may be a statement:

  • Read the input as a string value using Scanner.readLine();
  • Try converting the string to int using the Integer.parseInt method. This method will throw a NumberFormatException if the input string contains decimal and invalid digits.
  • if the input value is correctly analyzed in the previous step, then check the negative
+8
source
 System.out.println("Please enter a positive integer seed value: "); boolean flag = true; while(flag) { try { seedValue = Integer.valueOf(input.nextLine()); if (seedValue <= 0) { System.out.println("input is not a positive Integer "); System.out.println("Please enter a positive integer seed value: "); } else { flag=false; } } catch(NumberFormatException e) { System.out.println("input is not a positive Integer "); System.out.println("Please enter a positive integer seed value: "); } } 
+1
source

I assume that "input" is a Scanner class. If so, check out javadoc for the Scanner.nextInt () method. It throws a series of exceptions if no integer is entered. Therefore, you should put your call into a try try block and look for these exceptions. The next thing you can do, since it still allows them to enter a negative value, is simply to check if the input signal is less, and if so, display some message letting the user know that they should only enter a positive value.

0
source

Your attempt looks good, you allow the user to enter something, and then check to see if it matches the required form. If not, retry the user request or print an error or something else. If you want to make the program in some way, these letters, such as "-" char, do not even appear on the screen during input, you still have to read everything that the user entered, check, string (delete the illegal letter) and refresh the screen with every keystroke. This is probably not what you need to do here.

Imagine your common social sites when creating a profile with password requirements, for example, with one number, at least 6 letters, etc. You can enter everything you want into the text box only after the form is submitted, the program either accepts your input or redirects you to the same form with an error message to hint at the problem.

0
source

I'm not sure which input class is accurate, but I assume its reading of Scanner from the standard is included. If so, you can take advantage of the exceptions that were nextInt() , in particular InputMismatchException . Your code will be -

 int seedValue; double angle, gunpowder; System.out.println("Please enter a positive integer seed value: "); while(invalidInput){ try{ seedValue = input.nextInt(); if(seedValue <= 0) { System.out.println("Please enter a positive integer seed value: "); } else{invalidInput=false;} } catch(InputMismatchException e){ System.out.println("Please enter a positive integer seed value: "); } 

}

  System.out.println("That target is " + threeDec.format(gen.nextDouble() * 1000) + "m away."); 
0
source

Source: https://habr.com/ru/post/953832/


All Articles