Only let users input positive integers (no decimal

2019-02-22 06:07发布

问题:

I know how to ask the user to input positive integers, but I don't know how approach the code to avoid an input error such as a 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.");

回答1:

This may be an approch:

  • Read the input as a string value using Scanner.readLine();
  • Try to convert the string to int using Integer.parseInt method. This method will throw a NumberFormatException in case the input string contains decimals and invalid digits.
  • if the input value is parsed properly in previous step, then check for negative


回答2:

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: ");
  }

}


回答3:

I am assuming that "input" is the Scanner class. If so, check out the javadoc for the Scanner.nextInt() method. It throws a number of exceptions if an integer is not entered. So you should put your call to it in a try catch block and look for these exceptions. The next thing you can do since that still lets them enter a negative value is simply check if the input is less than 0 and if so, output some message letting the user know they must only enter a positive value.



回答4:

Your attempt looks fine, you let the user type something in and then check if it matches the required form. If not, proceed in asking the user again or output an error or whatever. If you want to make the program in a way, that letters, like the '-' char don't even appear on the screen while typing in, you still have to read whatever the user typed in, validate it, reform the string (delete the illegal letter) and refresh the screen with every single keystroke happening. This is probably not what should be done here.

Imagine your common social websites while creating a profile with password requirements like having one number, at least 6 letters and so on. You can type whatever you want in the textbox, only after submitting the form the program either accepts your input or redirects you to the same form with an error message to hint at the problem.



回答5:

Im not sure what class input is exactly, but I presume its Scanner reading from standard in. If this is the case, you can take advantage of the exceptions thrown by nextInt(), specifically InputMismatchException. Your code would 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.");