-3

I am trying to call the method gasCost from the main method. I give the user the choice of entering a number to bring up a certain calculation, in this instance the user has selected option 3. The integer gallons_Gas is defined in the main method, so I am passing that into gasCost method.

I get an error after the else statement and can't compile. Says .class expected where the variable name gallons_Gas starts and after gallons_Gas says ; expected. What am I doing wrong and how do I fix this?

public class Deryck_HW2_TripCalculatorMenu 

{

public static void main(String[] args)
{
    //Get a scanner instance
    Scanner userInput = new Scanner(System.in);

    //Ask for gallons of gas consumed
    System.out.print("Gallons of Gas Consumed: ");

    //Save gallons of gas consumed
    int gallons_Gas = userInput.nextInt();

    //Give user options menu
    menu();

    //Ask user choice
    System.out.print("Choose from options above (enter number 1-4): ");

    //Get user choice 1-4
    int choice = userInput.nextInt();

    //Test choice
    if (choice == 1)
    {
        //avgSpeed();
    }    
    else if (choice == 2)
    {
        //mpg();
    }    
    else if (choice == 3)
    {
        gasCost(int gallons_Gas);
    }   
    else
    {
        System.out.print("Error: selection invalid. Restart program and enter a number between 1 and 4.");
    }    
}


public static void gasCost(int gallons_Gas)
{
    //Get a scanner instance
    Scanner userInput = new Scanner(System.in);

    //Ask cost of gallon of gas
    System.out.print("What is the cost per gallon of gas? ");

    //Save cost per gallon
    double gallon_Cost = userInput.nextDouble();

    //Calculate total cost
    double total_cost = (gallons_Gas * gallon_Cost);

    System.out.print("Total cost of gas for this trip was $" + total_cost);

}
Deryck
  • 27
  • 1
  • 5

3 Answers3

1

I don't know if this is a pseudo-code or just the real code. If it is the real code, there are several mistakes:

gasCost(int gallons_Gas);

You should know the differences between formal parameters and actual parameters. In the actual parameters type of variable is not required, instead of formal parameters. Link:

What is a formal parameter in Java?

So, that code should be like:

 int gallons_gas = 5; //Just for example

 gasCost(gallons_Gas);

After that, you should listen to the guys in the comments: be sure where that else if statement is, if you put it in the wrong way it won't work.

Hope it helps

Community
  • 1
  • 1
FFdeveloper
  • 241
  • 3
  • 14
1

Try to understand what these lines mean and note how to call methods, how to pass in variables, when to declare variables etc...

import java.util.Scanner;
public class GasCalculator  {
    public static void main(String[] args)  {
        final int GALLONS_GAS = 100;    // change this to whatever. It's good practice to use constants for variables that does not need to be immuted.
        Scanner sc = new Scanner(System.in);
        int user_choice = -1;
        try {
            user_choice = sc.nextInt();
        } catch (Exception e)   {
            System.out.println("Only enter integers.");
            main(args);
            return;
        }
        switch(user_choice) {
            case 1:
                // do something.
                break;
            case 2:
                // do something.
                break;
            case 3:
                gasCost(GALLONS_GAS);
                break;
            default:
                System.out.println("Bad input");
                main(args);
                break;
        }
        sc.close();
    }
    public static void gasCost(int gallons_Gas) {
    //Get a scanner instance
    Scanner userInput = new Scanner(System.in);

    //Ask cost of gallon of gas
    System.out.print("What is the cost per gallon of gas? ");

    //Save cost per gallon

        // should try using a try-catch here to handle InputMismatchException
    double gallon_Cost = userInput.nextDouble();
    //Calculate total cost
    double total_cost = (gallons_Gas * gallon_Cost);

    System.out.print("Total cost of gas for this trip was $" + total_cost);
    userInput.close();
    return;
    }
}
noobcoder
  • 323
  • 1
  • 3
  • 12
  • Should I use a switch statement just because it's a little more neat than a bunch of if-else statements? – Deryck Apr 05 '17 at 21:19
  • @Deryck no you shouldn't use a switch case just because its neater. You should use Switch case because it's faster than a list of if-else if-else statements. The compiler can generate a Jump table where as every if else is tested. in your case its personal preference really, but just another way to showing you how you can selective execute code. – noobcoder Apr 05 '17 at 21:24
0
gasCost(int gallons_Gas);

should be

int gallons_Gas;
if (...) {
    gasCost(gallons_Gas);
}

You cannot declare an int within the parameter list of a method call.

kimbert
  • 2,376
  • 1
  • 10
  • 20