So, I've created a program that when a user inputs a type of vehicle, it will print how much will the user pay for their fare. Here's what I have so far:
How do I call the methods Vehicle, Vehicle1, etc.. so that I am able to print the fare costs?

package commuting;
import java.util.Scanner; 

public class Commuting {
    public static void main(String[] args) {
        String strVehicle;

        Scanner sc = new Scanner (System.in); 


        System.out.print ("What mode of transportation will you be taking to school? "); 
        strVehicle = sc.next();

        if (strVehicle.equals("Jeep") ){
            System.out.print ("You're fare will be: " );

        }


    }

    public static double Vehicle(String strVehicle){
        double dJeep = 8.00;
        return dJeep;

    }
    public static double Vehicle2(String strVehicle){
        double dLRT = 15.00;
        return dLRT;
    }
    public static double Vehicle3(String strVehicle){
        double dMRT = 30.00;
        return dMRT;
    }
    public static double Vehicle4(String strVehicle){
        double dBus = 30.00;
        return dBus;
    }
    public static double Vehicle5(String strVehicle){
        double dTaxi = 100.00;
        return dTaxi;
    }
    public static double Vehicle6(String strVehicle){
        double dPedicab = 20.00;
        return dPedicab;
    }
}

Recommended Answers

All 7 Replies

Those methods return a double value. You could assign the value to a double variable and use the println() or the printf() methods to print it.

Can you describe what the code you are writing is supposed to do? The VehicleN methods take a String argument but do nothing with it.

I would suggest to put your if statement in line 14 of your code to one of your Vehicle method. That way, the String argument in your method will make sense.

Example:

public static double Vehicle(String strVehicle){
    double rate = 0;

    if(strVehicle.equals("Jeep")){
        rate = 8.00;
    } else if (strVehicle.equals("Taxi"){
        rate = 100.00;
    }

    //etc. etc.

    return rate;
}

This case, you will not need to declare Method for each vehicle. Your if then statement at line 14 will now look like:

System.out.print("Your fare will be: " + Vehicle("Jeep"));

Hope this helps.

why you have created so many methods for matching user input?

i think there is only one method is required for all that.
I appreciate suggestion given by "dimasalang".
Just go with it.
It will work perfectly.

Use the following program code. It works perfectly with the intended inputs

package commuting;
import java.util.Scanner;

public class Commuting {

    private static String vehicle;

    public static double typeVehicle(String vehicle) {

        if(vehicle.equals("Jeep")) {
            return 8.00;
        }
        if(vehicle.equals("Sedan")) {
            return 5.00;
        }else
            return 3.00;

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        vehicle = scan.nextLine();
        System.out.println("Your fare is "+ typeVehicle(vehicle));

    }

}

Kennie, just a few remarks... seeing as this is a 5 year old thread, was it really necessary to bring it back?

secondly, since you have vehicle as a variable on class scope, why do you still pass it as a parameter to your method?

for bonuspoints: can you find the redundant keyword in your code?

OK, redundant keyword?

the else is not needed, since either the if condition evaluates to true, and return 5.00; is executed, or it skips the if-block and goes to the next statement.

With or without the else, the flow is identical.

commented: Right! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.