Search code examples
javainputmismatchexception

Java InputMismatchException Error


I've been given a task to make some conversions from ft and in to cm. I've gotten most of this down and the conversions do work. I also want to include the statement of A negative number... or A non-digit... when I type a string, for example, or a negative number, to display said message.

The problem I am getting is that when I do type up a string or negative number, I get the output of testProgram.NegativeNumberException when I enter -9, for example. And testProgram.NonDigitNumberException, when I enter joe, for example.

I am thinking there is something wrong in the catch but not sure exactly where it won't click.

package testProgram;
import java.util.InputMismatchException;
import java.util.Scanner;

public class conversion{

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       double cm = -1;
       while(cm == -1){
       cm = convertToCentimeters(scan);

       if(cm!=-1){
       System.out.println("Your result = " +cm);
       }
       else{
           System.out.println("Please enter the values again.");
       }
       scan.nextLine();
       }
   }
   public static double convertToCentimeters(Scanner scan){
       double centimeters = -1;
       try{
           double foot = getFootValue(scan);
           double inch = getInchValue(scan);
           double totalInches = foot * 12 + inch;
           centimeters = totalInches * 2.54;

           }catch(NegativeNumberException e1){
               System.out.println(e1);
           }
           catch(NonDigitNumberException e2){
               System.out.println(e2);
           }
           return centimeters;
   }
   public static double getFootValue(Scanner scan) throws NegativeNumberException, NonDigitNumberException{
       try{
       System.out.println("Enter the foot value: ");
       double foot = scan.nextDouble();
       if(foot <= 0){
           throw new NegativeNumberException ("A negative foot value has been entered.");
       }
       return foot;
       }
       catch(InputMismatchException e){
           throw new NonDigitNumberException ("A non-digit foot value has been entered.");
       }
   }
   public static double getInchValue(Scanner scan)throws NegativeNumberException, NonDigitNumberException{
   try{
       System.out.println("Enter the inch value: ");
       double inch = scan.nextDouble();
       if(inch <= 0){
           throw new NegativeNumberException ("A negative inch value has been entered.");
       }
       return inch;
       }
       catch(InputMismatchException e){
           throw new NonDigitNumberException ("A non-digit inch value has been entered.");
       }
   }
}

Solution

  • Alternatively to @Scary's suggestion, you can add a constructor to your custom exceptions as -

    NegativeNumberException(String str) {
        System.out.println(str);
    }
    

    This shall help you print the message when you

    throw new NegativeNumberException ("A n....");