I'm having problems when the user enters "L" or "%" as an example (error message is displayed correctly, because only numbers should be allowed to enter) ,however after that the next input message "Please enter Average impulse delivered" comes up straight away. I wish to make it stop until the user enters a number and Then the next one comes up. Sorry if im not clear, please feel free to ask.
//UserEntry class
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class UserEntry{
public double totalImpulse ;
public double averageImpulse;
//Declaring all user input, with exception handling
double totalImpulse(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// String for Total Impulse
String strtotalImpulse = null;
int inttotalImpulse = 0;
try {
strtotalImpulse = in.readLine();
}
catch (IOException ioe) {
}
// convert it to integer
try {
double totalImpulse=Double.valueOf(strtotalImpulse); //Exception handling (Only integers)
}
catch (Exception e) {
System.out.println("Please enter an Integer for Total Impulse!"); //Print this when "L" or "%" , not integer has been entered.
}
return inttotalImpulse;
}
double averageImpulse(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// String for Total Impulse
String straverageImpulse = null;
int intaverageImpulse = 0;
try {
straverageImpulse = in.readLine();
}
catch (IOException ioe) {
}
// convert it to integer
try {
double averageImpulse=Double.valueOf(straverageImpulse); //Exception handling (Only integers)
}
catch (Exception e) {
System.out.println("Please enter an Integer for Average Impulse!"); //Print this when "L" or "%" , not integer has been entered.
}
return intaverageImpulse;
} }
//Master class (Input)
public class Master { //Master class
public static void main( String args[] ) //Standard header for main method
{
//User inputs
UserEntry input = new UserEntry(); //Creating object from UserEntry class
System.out.print("\nPlease enter Total impulse delivered: ");
input.totalImpulse();
System.out.print("Please enter Average impulse delivered: ");
input.averageImpulse();
}
}