-1

and i just created a program for fare with discount. but I dont know where to put the try and catch.

this is the program without try and catch

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        
            Scanner input = new Scanner(System.in);
            String passengerType;
            double distance;
            double minimumFare = 20;
            double fare1, finalFare;
                
            System.out.println("Enter the type of Passenger (Ordinary/Student/Senior): ");
            passengerType = input.nextLine();
            System.out.println("Enter the Distance: ");
            distance = input.nextDouble();
            
            // Condition for "Ordinary Passenger"
            if (passengerType.equalsIgnoreCase("Ordinary"))
            {
                if (distance <= 10)
                {
                    System.out.println("Your Fare is: "+minimumFare);
                }
                else if (distance > 10)
                {
                    fare1 = (distance - 10) * 2.50;
                    finalFare = fare1 + minimumFare;
                    System.out.println("Your Fare is: "+finalFare);
                }
            }
            // Condition for "Student Passenger"
            else if (passengerType.equalsIgnoreCase("Student"))
            {
                if (distance <= 10)
                {
                    finalFare = 20 - (20 * 0.20);
                    System.out.println("Your Fare is: "+ finalFare);
                }
                else if (distance > 10)
                {
                    fare1 = ((distance - 10) * 2.50);
                    finalFare = fare1 + 20 - ((fare1 + 20) * 0.20);
                    System.out.println("Your Fare is: "+finalFare);
                }
            }
            // Condition for "Senior Passenger"
            else if (passengerType.equalsIgnoreCase("Senior"))
            {
                if (distance <= 10)
                {
                    finalFare = 20 - (20 * 0.30);
                    System.out.println("Your Fare is: "+ finalFare);
                }
                else if (distance > 10)
                {
                    fare1 = ((distance - 10) * 2.50);
                    finalFare = fare1 + 20 - ((fare1 + 20) * 0.30);
                    System.out.println("Your Fare is: "+ finalFare);
                    
                }
            }
           
        
    }
}

the output of the program must be these.(when error input) enter image description here

enter image description here

thank you so much in advance, its my first time in java language. please don't vote negative ^_^

Carlmalone
  • 21
  • 5
  • 2
    FYI this is not the correct use for exceptions. This is not an exceptional situation, this is just wrong input that, in your case, should be handled with a simple `else`. – Federico klez Culloca Mar 25 '21 at 11:13

3 Answers3

1

I'm also new at this, but if you were to run the programme, and try to put in a different input than suggested. For example putting string where you need an int or putting an int where you need string, when you run the programme the compiler will show you the exceptions which you need to catch. I've altered the start of the programme, just to show you how i'd do it, i'm also not the best but hopefully this puts you on the right track

 Scanner input = new Scanner(System.in);
    String passengerType = null;
    double distance = 0;
    double minimumFare = 20;
    double fare1, finalFare;

    try {
        System.out.println("Enter the type of Passenger (Ordinary/Student/Senior): ");
        passengerType = input.nextLine();
        System.out.println("Enter the Distance: ");
        distance = input.nextDouble();
    } catch (InputMismatchException e){
        System.out.println("Please enter strings for passenger and numbers for distance " + e);
    }

also check out the link When should an IllegalArgumentException be thrown?

Tomas
  • 11
  • 3
1

Your code never throws any Exception. try and catch blocks are used to catch Exceptions that may be thrown when calling methods that throw them (you can see it on method declaration). If you want to output that an argument is invalid, add an else statement after your conditions, and throw an IllegalArgumentException:

else {
    throw new IllegalArgumentException("You did something wrong");
}

Or if you want a "cleaner" error, output it to System.err, so that the user doesn't need to see the stack trace:

else {
    System.err.println("Invalid Passenger Type");
}

The same goes to checking if distance is a String, like the other answer showed.

CLR 123
  • 161
  • 1
  • 8
1

In this case, you are making use of a Scanner which needs to be closed after use, so it is best to go with a try-with-resources statement which will take care of automatically closing the Scanner when it is done. Also, in order to ensure valid input is gotten, I have included an input checker to keep reading until a valid string is entered for Passenger and a Distance >= 0 is entered.

In the case of Distance, using the input.nextDouble() ensures the input is a valid number and will throw an InputMismatchException if it is not a valid number. Consider reading the input as a String and parse it to Double, that way you have more control over what happens and can demand a new input without the program being terminated. The way it is currently, the program will get terminated as there is no way to read a new input after displaying the error message.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        
        String passengerType;
        double distance;
        double minimumFare = 20;
        double fare1, finalFare;
            
        try(Scanner input = new Scanner(System.in);){
            System.out.println("Enter the type of Passenger (Ordinary/Student/Senior): ");
            passengerType = input.nextLine();
            while(passengerType == null || passengerType.trim().equals("") || (!passengerType.equals("Ordinary") && !passengerType.equals("Student") && !passengerType.equals("Senior"))){
                System.out.println("Valid Passengers are Ordinary/Student/Senior: ");
                passengerType = input.nextLine();
            }
            System.out.println("Enter the Distance: ");
            distance = input.nextDouble();
            while(distance < 0){
                System.out.println("Distance must be greater than or equal to 0: ");
                distance = input.nextDouble();
            }
            System.out.println("Input read: " + passengerType + ", " + distance);
        } catch (InputMismatchException e){
            System.out.println("Distance must be a number");
            return;
        } catch (Exception e){
            e.printStackTrace();
            return;
        }
            
            // Condition for "Ordinary Passenger"
            if (passengerType.equalsIgnoreCase("Ordinary"))
            {
                if (distance <= 10)
                {
                    System.out.println("Your Fare is: "+minimumFare);
                }
                else if (distance > 10)
                {
                    fare1 = (distance - 10) * 2.50;
                    finalFare = fare1 + minimumFare;
                    System.out.println("Your Fare is: "+finalFare);
                }
            }
            // Condition for "Student Passenger"
            else if (passengerType.equalsIgnoreCase("Student"))
            {
                if (distance <= 10)
                {
                    finalFare = 20 - (20 * 0.20);
                    System.out.println("Your Fare is: "+ finalFare);
                }
                else if (distance > 10)
                {
                    fare1 = ((distance - 10) * 2.50);
                    finalFare = fare1 + 20 - ((fare1 + 20) * 0.20);
                    System.out.println("Your Fare is: "+finalFare);
                }
            }
            // Condition for "Senior Passenger"
            else if (passengerType.equalsIgnoreCase("Senior"))
            {
                if (distance <= 10)
                {
                    finalFare = 20 - (20 * 0.30);
                    System.out.println("Your Fare is: "+ finalFare);
                }
                else if (distance > 10)
                {
                    fare1 = ((distance - 10) * 2.50);
                    finalFare = fare1 + 20 - ((fare1 + 20) * 0.30);
                    System.out.println("Your Fare is: "+ finalFare);
                    
                }
            }
           
        
    }
}