0

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();
}
}
  • (An exception has to be thrown for it to be related to exception handling; invalid user input is not [yet] an exception.) – user2864740 Mar 15 '15 at 00:58
  • 1
    *"anything apart from Integers (1, 2, 5.05, 10.10)"* 5.05 and 10.10 are not integers. Please clarify. – Radiodef Mar 15 '15 at 02:08

2 Answers2

0
while(true){
    try { 
        strtotalImpulse = in.readLine(); 
    } catch (IOException ioe) { }
    try{
        double yourReturnValue=Double.valueOf(strtotalImpulse);
        return yourReturnValue; //we get a double
    }catch (Exception e) { //oops... there is something wrong when trying to cast string to double. So it must be wrong with user input.
        System.out.println("please enter a number:");
    }
}
ch271828n
  • 5,683
  • 3
  • 24
  • 44
  • Thanks so much! Lastly, how could I stop it from getting onto the next input, until the user enters a number? So it will not allow to progress on until the input is a number. – Viktor Szabad Mar 15 '15 at 01:19
  • @ViktorSzabad the code does exactly that - it's stuck in a while loop, until the user enters a number and it returns - if the user doesn't enter a number, an exception is thrown before the return and it asks the user to ether a number again. – mk. Mar 15 '15 at 01:27
  • 1
    While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown Mar 15 '15 at 01:33
  • When I enter a "L" for the totalImpulse, it gives me an error, however it jumps onto the next line as well: Please enter Total impulse: L Please enter a number Please enter Average impulse: – Viktor Szabad Mar 15 '15 at 02:04
0

This solution is very similar to Turtle one, but removing a bit of duplicated code, and forcing the MasterClass to wait for a value. I changed a bit the logic, and for read input used a scanner as seen on this question How can I read input from the console using the Scanner class in Java?

Master Class

public static void main( String args[] ) //Standard header for main method
 {
            //User inputs
UserEntry input = new UserEntry(); //Creating object from UserEntry class


Double totalImpulse = null;
while(totalimpulse==null){

    System.out.print("\nPlease enter Total impulse delivered: ");
    try { 
     totalimpulse=  input.getDoubleFromConsole();
 }
 catch(Exception e){
System.out.println("please enter a number:");
}
}
Double averageImpulse=null;
while(averageImpulse==null){
    System.out.print("Please enter Average impulse delivered: ");

    try{
        averageImpulse= input.getDoubleFromConsole();
    }
    catch(Exception e){
System.out.println("please enter a number:");
}
}
}

UserEntry:

  double getDoubleFromConsole(){

             Scanner sc = new Scanner(System.in);
            double i = sc.nextDouble();

    }
Community
  • 1
  • 1
Ricardo Umpierrez
  • 728
  • 2
  • 8
  • 24
  • Thanks. It worked, however I still cannot be able to enter a new value. Its successfully forcing the class to wait for a value, but then it wont allow me to re-enter after invalid input. – Viktor Szabad Mar 15 '15 at 15:31
  • Updated withouth duplicated code, and all the logic of the retry in the main class that should work. – Ricardo Umpierrez Mar 15 '15 at 15:58
  • I'm a bit confused with you updated version. I want all the exception handling within my UserEntry class not the Master class, – Viktor Szabad Mar 15 '15 at 16:57
  • Is there a way to put the try&catch function into the UserEntry class and call it from the main (Master) ? – Viktor Szabad Mar 15 '15 at 19:13