Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java input validation with regular expression

807588Mar 28 2009 — edited Mar 28 2009
I am trying to make sure the user can only enter numbers 0 - 100, can any of you Java geniuses give me a hint. My regex matches the inputs I want, but when I enter a '9' I encounter an error when I cast the string to a double. Actually the input value is never assigned to the perc variable, but only when the input is a single digit. Here' s the piece of code that's giving me this headache:
while(!in.hasNext("[0-9]")){
     try {
          perc = in.next("[0-9]{1,2}|100");
          break;
     } catch (InputMismatchException e) {
          System.out.println("Bad Input - Try Again");
          perc = in.next();
     }
}
dblPerc = Double.parseDouble(perc);
And here's the error:
Exception in thread "main" java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
        at java.lang.Double.parseDouble(Double.java:510)
        at applicant_database.View.showInput(View.java:46)
        at applicant_database.View.main(View.java:101)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 25 2009
Added on Mar 28 2009
1 comment
319 views