Java - Exception handling - How to re-enter invali

2019-06-07 05:18发布

问题:

The exception handling only accepts double inputs. Therefore when the user enters "k" for example, it will say "Error! Please enter a number!". However instead of allowing the user to re-enter the input, it jumps onto the next input "Average impulse) How can I make it work so it will stay on the same line and allow to re-enter a value?

//Main class

        public class Main { //Master class


        public static void main( String args[] ) //Standard header for main method
        {


        kbentry input = new kbentry(); //Creates object of kbentry class

        System.out.print("\nPlease enter a number for Total Impulse: " ); //Print message to enter 1. input
        double totalImpulse = input.totalImpulse1(); //Holds the variable entered
        System.out.println("You have entered : " + totalImpulse); //Shows the variable entered


        System.out.print("\nPlease enter a number for Average Impulse: " ); //Print message to enter 2. input
        double averageImpulse = input.averageImpulse2(); //Holds the variable entered
        System.out.println("You have entered : " + averageImpulse); //Shows the variable entered
        }
        }

//kbentry class

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;


public class kbentry{ //Class name

double totalImpulse1(){ //Method for 1. input

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));     //Creates BufferedReader object for System.in

//Total Impulse entry
String strTotalImpulse = null;  // These must be initialised
double    intTotalImpulse = 0; //Setting it double

try {
  strTotalImpulse = in.readLine(); //Reads string value from the keyboard
} 
catch (IOException ioe) {  // ignore exception

   }

try {
  intTotalImpulse = Double.parseDouble(strTotalImpulse);  // convert it to double
} 
catch (NumberFormatException nfe) {
   System.out.println("Error! Please enter a number!" + nfe.toString()); //Error message if its not a double

}


return intTotalImpulse; //return value
}



double averageImpulse2(){ //Method for 2. input

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));//Creates BufferedReader object for System.in

// String for AverageImpulse
String strAverageImpulse = null; // These must be initialised
double    intAverageImpulse = 0; //Setting it double

try {
strAverageImpulse = in.readLine(); //Reads string value from the keyboard
} 
catch (IOException ioe) {   // ignore exception
}
// convert it to integer
try {
  intAverageImpulse = Double.parseDouble(strAverageImpulse); // convert it to double
}
catch (NumberFormatException nfe) {
System.out.println("Error! Please enter a number!" + nfe.toString()); //Error message if its not a double

}
return intAverageImpulse; //return value
}
}

回答1:

If you user enter other than double you will get NumberFormatException in that cause you have to simply call that method again in you

catch (NumberFormatException nfe) { 
    System.out.println("Error! Please enter a number!" + nfe.toString());  //Error message if its not a double
    //again ask for input
    System.out.print("\nPlease enter a number for Total Impulse: ");
    return totalImpulse1();
}