Error: <identifier> expected for line 72
which is
public static double calculatePrice(choice, t)
if i leave it as calculatePrice (int, int)
i still get the same error, any help or idea how to fix this
i've tried changing the method to if i leave it as calculatePrice (int choice, int t) but that just creates more errors.
import java.util.Scanner;
public class KosakowskiTickets3
{
public static void main(String[] args )
{
Scanner key = new Scanner (System.in);
double price;
double total;
int choice;
int t;
displayPrices();
getStadium(choice);
getNumTickets(t);
calculatePrice(choice, t);
displayTotal();
}
//Prices
public static void displayPrices()
{
System.out.println("Welcome to the US Open");
System.out.println("New York is where dreams come true\n");
System.out.println("Ticket prices are: ");
System.out.println("\t1. Arthur Ashe: \t\t$450.25");
System.out.println("\t2. Louis Armstron: \t\t$225.75");
System.out.println("\t3. Grandstand: \t\t$175.50\n");
}
// user choice
public static int getStadium();
{
Scanner key = new Scanner(System.in);
int choice;
System.out.println("Please choice 1, 2, or 3: ");
choice = key.nextInt();
while (choice <= 3)
{
System.out.println("Please enter a valid choice.");
choice = key.nextInt();
}
return choice;
}
// Tickets
public static int getNumTickets()
{
Scanner key = new Scanner (System.in);
int t;
System.out.print("Please note that tickets are limited to 10 per stadium. \nEnter number of tickets you would like to purchase: ");
t = key.nextInt();
while (t <=10)
{
System.out.println("Please enter a valid number of tickets.");
t = key.nextInt();
return t;
}
}
// Price math
public static double calculatePrice(int, int)
{
int choice;
int t;
switch (choice)
{
case 1:
price = 225.25;
total = price * t;
break;
case 2:
price = 117.75;
total = price * t;
break;
case 3:
price = 145.50;
total = price * t;
break;
default:
System.out.println("That is not a choice.");
double price;
double total;
return total;
}
}
// Price Total
public static void displayTotal(double total)
{
System.out.println("Your total ticket price is $" total);
}
}
CodePudding user response:
Because you have typoes in your source code.
public static int getStadium(); {
That semicolon at the end there is illegal. Get rid of it.
public static double calculatePrice(int, int)
That is not a valid method declaration. Parameters are of the form Type name
, so:
public static double calculatePrice(int choice, int t) {
... code here
}
For example. You're saying: Anybody that calls this calculatePrice
method must do so by writing calculatePrice(a, b)
where a and b are expressions; both a and b are of type int. Okay, the values of these expressions resolve to are passed to your calculatePrice
method - how would you like to refer to them inside the code of calculatePrice
? You have to pick a name. That's what choice
and t
are about. You then declare 2 local vars inside your calculatePrice
. I bet you don't want that.
Parameters are exactly local local variables, except, set by the caller and not by you:
public static int example1(int a, int b) {
return a b;
}
public static int example2() {
int a = 5;
int b = 10;
return a b;
}
These methods are nearly identical, but one adds the 2 parameters given, the other doesn't have parameters. They act otherwise identically - both a
and b
are local variable names inside these two methods.
CodePudding user response:
There're several errors in your code.
Error 1:
In the parameter of the method 'calculatePrice' you didn't give any name. Naming those 2 parameter will solve the problem.
You need to give name as well as their data type.
Error 2:
You didn't set any value to the variable 'choice', 't' before sending these variables to related methods as parameters.
Initialize these variables with a value before using them
Error 3:
From main method you're calling the method 'getStadium
' like this =>
getStadium(choice);
But according to the definition of this method, it doesn't take any parameter.
Besides, from the implementations, it seems this method is not supposed to take any parameter.
I guess you tried to take input in this method, then return it to the main method to store as the variable 'choice'.
For that, you need to write like this =>
choice = getStadium();
You did this same mistake when calling the method 'getNumTickets
' from the main method.
Call it like this =>
t = getNumTickets();
Error 4:
In the definition of the method 'getStadium
' there's a syntax error.
You're not supposed to give a semicolon (;) at the end of the method name.
public static int getStadium();
remove the semicolon you added at the end.
Let's see some of the logical errors.
Error 5: Method getStadium
Here you're taking input from the user as long as the input is from 1 to 3. But in the loop's condition, you're checking whether the input is within 3.
So it should be like this =>
public static int getStadium() {
Scanner key = new Scanner(System.in);
int choice;
System.out.println("Please choice 1, 2, or 3: ");
choice = key.nextInt();
while (choice <1 || choice > 3) {
System.out.println("Please enter a valid choice.");
choice = key.nextInt();
}
return choice;
}
Error 6: Method getNumTickets
In this method, you made kind of similar mistake like error5.
Valid input is from 1 to 10.
You tried to run the while loop as long as input is <= 10.
Another mistake you did in here is, you returned the value from the while loop.
This method is supposed to be like this =>
public static int getNumTickets() {
Scanner key = new Scanner(System.in);
int t;
System.out.print("Please note that tickets are limited to 10 per stadium. \nEnter number of tickets you would like to purchase: ");
t = key.nextInt();
while (t < 1 && t > 10) {
System.out.println("Please enter a valid number of tickets.");
t = key.nextInt();
}
return t;
}
CodePudding user response:
first of all you must give name for the method parameter type then if the method is of type return(double) you can not print message in it finally if you want to declare variables and use them in the method you should do that at first. so the code is:
package stackoverflow;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
double result = calculatePrice(4,1);
if (result != -1)
System.out.println(result);
else
System.out.println("That is not a choice.");
}
public static double calculatePrice(int choice, int t)
{
double price = 0;
double total = 0;
switch (choice)
{
case 1:
price = 225.25;
total = price * t;
return total;
case 2:
price = 117.75;
total = price * t;
return total;
case 3:
price = 145.50;
total = price * t;
return total;
default:
return -1;
}
}
}
Sorry about my English
CodePudding user response:
It is quite a while since I used Java but shouldn't you initialize your variables? So for example "int choice = t".
This might help you: https://rollbar.com/blog/how-to-handle-the-identifier-expected-error-in-java/#