Q:

How can I validate a numeric input in java?

I've validated that the entered string is a number.
while (true) {

    try {
        numberOfmiles = Double.parseDouble(JOptionPane.showInputDialog(null," Enter Positive Number"));       
         break;
    }

    catch (NumberFormatException nfs) {
         JOptionPane.showMessageDialog(null, "Please Enter a Positive number");
    }
}

I need to also check that it is a positive number.  How would I do that?

A:

        while(true){
      String userInput=JOptionPane.showInputDialog(null," Enter Positive Number");
        if(userInput.matches("^\\d+(.\\d+)?$")){
         double numberOfmiles=Double.parseDouble(userInput);
         if (numberOfmiles <0){
        JOptionPane.showMessageDialog(null, "Warning, enter positive numbers only")
        }
        else{
        //successful input
        break;
        }
    }
        else{
            JOptionPane.showMessageDialog(null, "Just positive numbers are allowed"); 
        }

   }

