1

I'm new here and to programming, so sorry if this is too long. I'm sure there's a much easier way to do this, but I'd really like to see if this can get resolved.
I'm working on a program that takes user input (double) and converts from Celsius to Fahrenheit (cToF) and vice versa (fToC) with constraints of 0 to 1000. I have a separate method for both cToF and fToC.
I want to throw an exception for when the user doesn't input a double. I have catch (InputMismatchException cToF) and catch (InputMismatchException2 fToC) in the main that do different things.
Also, I'm suspicious that the math is incorrect for fToC, I just haven't gotten to it yet.

I tried changing the name of InputMismatchException2 to InputMismatchException, but I get "exception has already been caught". I already have import java.util.*.
I tried adding "throws Exception" at the beginning of the main method and both of the conversion methods.
I'm not sure if the try-catch should be in the main, the individual methods, or both.

public static void main(String args[]) throws InputMismatchException, InputMismatchException2 {

    Scanner choice = new Scanner(System.in);
    String unit = "n";
    boolean typo = false;
    double tempC = 0;
    double tempF = 0;

    try {
        start();//method to determine conversion type (F-C or C-F)

        if (typo == false) {

            do {//forever loop

                question(unit);

            }//end do loop
            while (typo == false);

        }//end if

        System.out.println("Thank you, goodbye!");

    }//end try

    catch (InputMismatchException cToFE) {//if a non-double is entered

        System.out.println("The value " + tempC + " is not within previously defined constraints.");
        System.out.println("Please enter a new value.");
        cToF(unit);

    }//end cToF catch

    catch (InputMismatchException2 fToCE) {//if a non-double is entered

        System.out.println("The value " + tempF + " is not within previously defined constraints.");
        System.out.println("Please enter a new value.");
        fToC(unit);

    }//end fToC catch

}//end main method

public static void cToF(String unit) throws InputMismatchException {

    System.out.println();
    System.out.println("Please enter a value from 0 to 1000.");//CONSTRAINTS: 0 <= temp <= 1000

    Scanner temp = new Scanner(System.in);
    double tempC = temp.nextDouble();

    if (0 <=  tempC && tempC <= 1000) {//if tempC is within constraints

        double tempF = (tempC * 1.8) + 32;//arithmetic with tempC == tempF

        System.out.println(tempC + " degrees Celsius is " + tempF + " degrees Fahrenheit");//print conversion

    }//end if
    else if (tempC < 0 || tempC > 1000) {//if tempC is NOT within constraints

        System.out.println("The value " + tempC + " is not within previously defined constraints.");
        System.out.println("Please enter a new value.");
        cToF(unit);

    }//end else if
    else {//if non-double entered

        //InputMismatchException cToFE = new InputMismatchException();
        throw new InputMismatchException();

    }//end else

}//end cToF method

public static void fToC(String unit) throws InputMismatchException2 {

    System.out.println();
    System.out.println("Please enter a value from 0 to 1000.");//CONSTRAINTS: 0 <= temp <= 1000

    Scanner temp = new Scanner(System.in);
    double tempF = temp.nextDouble();

    if (0 <=  tempF && tempF <= 1000) {//if tempF is within constraints

        double tempC = (32 - tempF) / 1.8;//arithmetic with tempF == tempC
        System.out.println(tempF + " degrees Fahrenheit is " + tempC + " degrees Celsius");//print conversion

    }//end if
    else if (tempF < 0 || tempF > 1000) {//if tempF is NOT within constraints

        System.out.println("The value " + tempF + " is not within previously defined constraints.");
        System.out.println("Please enter a new value.");
        fToC(unit);

    }//end else if
    else {//if non-double entered

        //InputMismatchException2 fToCE = new InputMismatchException2();
        throw new InputMismatchException2();

    }//end else

}//end fToC method

What I want:
If the user types in a letter while in either method (fToC/cToF), print The value [tempF/tempC respectively] is not within previously defined constraints. Please enter a new value.
Then recursively call either method to restart the temp input process.

What I'm getting (with current code):

$ javac Temperature.java
Temperature.java:5: error: cannot find symbol
    public static void main(String args[]) throws InputMismatchException, InputMismatchException2 {
                                                                          ^
  symbol:   class InputMismatchException2
  location: class Temperature
Temperature.java:125: error: cannot find symbol
    public static void fToC(String unit) throws InputMismatchException2 {
                                                ^
  symbol:   class InputMismatchException2
  location: class Temperature
Temperature.java:42: error: cannot find symbol
        catch (InputMismatchException2 fToCE) {//if a non-double is entered
               ^
  symbol:   class InputMismatchException2
  location: class Temperature
Temperature.java:151: error: cannot find symbol
            throw new InputMismatchException2();
                      ^
  symbol:   class InputMismatchException2
  location: class Temperature
4 errors
castiyeaux
  • 13
  • 3
  • I call cToF/fToC in the start() method in the beginning. My understanding is that while it is in the cToF method and throws the exception, I can then have the catch print the error message and then call the method again, correct? That's why I was hoping that InputMismatchException vs InputMismatchException2 would help. – castiyeaux Aug 13 '19 at 02:02

1 Answers1

0

In short, no, you cannot do this. Otherwise, it would be impossible to decide the correct catch statement to execute upon an exception being thrown.

Your error here is "error: cannot find symbol". According to the error traces, this means that the symbol InputMismatchException2 cannot be found. Essentially, you will need to define a separate class InputMismatchException2 (extending InputMismatchException) if you want to throw a InputMismatchException2. The reason that you could then run your code would be that you would literally be throwing and catching two different kinds of InputMismatchException, InputMismatchException and InputMismatchException2.

However! You will probably be much better off initializing an InputMismatchException with a message, and using its getMessage method to extract the relevant information in the catch statement.

Looking closer at the code, it's generally not recommended to use exceptions as part of control flow.

Avi
  • 2,359
  • 1
  • 9
  • 21
  • Do I initialize it in the main method? Inside or outside the try? Because it's not a new method, right? – castiyeaux Aug 13 '19 at 02:15
  • Initialize what? The exception? `throw new InputMismatchException("something didn't match!");` will do the trick. – Avi Aug 13 '19 at 02:17
  • When you do `catch(InputMismatchException ime) { ... }`, you want to use the value inside the exception as `ime.getMessage()` – Avi Aug 13 '19 at 02:19
  • @castiyeaux I've made a quick demo [here](https://repl.it/repls/GrizzledWidePatch). What will print is: `this will print` and `Something went wrong: something didn't match!` – Avi Aug 13 '19 at 02:23
  • 1
    I see! The last two comments helped a lot. Thank you so much! – castiyeaux Aug 13 '19 at 02:29