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;
5 Years
Discussion Span
Last Post by jalpesh_007

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.

Edited by NormR1


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.


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

        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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.