0

I am learning Java and Exceptions.

I have been writing a code for invoice amount calculation with custom and not custom Exceptions.

When I run the code I have always an error message which appears to the terminal before entering a second value.

Enter a price 25
Enter 1 for normal price, 2 for interm. and 3 for reduce = 1
Normal price
Enter a price The input is not a number
Enter a price

I do not see where in my code something goes wrong. Why the sentence

The input is not a number

appears while no further number has been selected yet?

Is it a value that I have forgotten to reassign ? Is it something wrong in the construction of the Exception ?

Thank you in advance

Custom exception class

public class ChoiceSelectionException extends Exception {

    public ChoiceSelectionException() {
        super("Invalid number, it should be between 1 and 3");
    } 
}

Main class

import java.util.Scanner;

public class InvoiceAmount {
    public static void main(String[] args)
    {
        double normal = 0.2;
        double intermediate = 0.1;
        double reduce = 0.055;
        double price = -1;
        int choice = -1;
        double exclusiveOfTax = 0;
        double inclusiveOfTax = 0;
        Scanner myObj = new Scanner(System.in);

        while (price != 0) {
            boolean correctInput = false;
            while (!correctInput) {
                try {
                    System.out.print("Enter a price ");
                    String value = myObj.nextLine();
                    price = Double.parseDouble(value);
                    correctInput = true;
                } catch (Exception e) {
                    System.out.println("The input is not a number");
                }
            }

            correctInput = false;
            while ((!correctInput) && (price != 0)){
                try {
                    System.out.print("Enter 1 for normal price, 2 for interm. and 3 for reduce = ");
                    choice = myObj.nextInt();
                    if (choice < 1 || choice > 3) {
                        throw new ChoiceSelectionException();
                    }
                    correctInput = true;
                } catch (ChoiceSelectionException e) {
                    System.out.println(e.getMessage());
                }
            }

            if(choice == 1) {
                System.out.println("Normal price");
                exclusiveOfTax += price;
                inclusiveOfTax += price + price * normal;
            } else if (choice == 2) {
                System.out.println("Intermediate price");
                exclusiveOfTax += price;
                inclusiveOfTax += price + price * intermediate;
            } else {
                System.out.println("Reduce price");
                exclusiveOfTax += price;
                inclusiveOfTax += price + price * reduce;
            }
        }

        System.out.println("The total exclusive price is " + exclusiveOfTax + " euros");
        System.out.println("The total inclusive price is " + inclusiveOfTax + " euros");
    }
}

0 Answers0