Reputation: 317
I am a little stuck. I have grasped the concept of methods; understanding their purpose but still unsure on how to code it. Below is part of my code I kindly need guidance on. I am trying to create a method which can hold roles and depending on which switch option is selected it will return the the amount. I have four employment roles which all have different payscales. I have commented the errors on the role method. If I can correct this and make it work then I can complete this program :)
import java.util.Scanner;//allows input from the keyboard
import java.text.*;
class Wage_Method_2
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("0.00");
double normHrs = 0, overHrs = 0, bonusHrs = 0, actualHrs = 0, cash=0, overPay =0, bonusPay = 0, grossWage = 0,vat23 = 0, netWage =0;
String empName,nextEMP = "y", manager, superVisor, teamLead, general;
while (nextEMP.equalsIgnoreCase("y"))
{
System.out.print("Please enter an employee's first name: ");
empName = input.next();//their name
System.out.print("Please enter "+empName+"'s employment ID: ");
int job = input.nextInt();
System.out.print("Please enter their total hours worked: ");
actualHrs = input.nextDouble();
double basicPay = rate (5.75)*actualHrs;
System.out.println("Basic Pay: "+basicPay);
System.out.println("\nWould you like to log hours for another employee?");
nextEMP = input.next();
if (nextEMP.equalsIgnoreCase("n"))
{
System.out.println("Thank you, the hours have been successfully logged.");
}
}// end of whileloop
}// end of class
public static double rate (double cash String jobrole)// when compiling these errors appear: error: ')' expected and error: <identifier> expected
{
switch (jobrole)
{
case 1:
cash = 5.75;
case 2:
cash = 5.75*1.1;
case 3:
cash = 5.75*1.25;
case 4:
cash = 5.75*1.42;
break;
}
return (cash);
}
}// end of program
Upvotes: 1
Views: 52
Reputation: 9648
There are a few errors here:
switch
with a String
and have the values be integers. (Also note switching on strings was first introduced in Java SE7.).
public static double rate (double cash, String jobrole)
// ^
{
switch (jobrole)
{
case "1": return 5.75;
case "2": return 5.75*1.1;
case "3": return 5.75*1.25;
case "4": return 5.75*1.42;
default: return 0; // some default.
}
}
Others have stated that the method has to be in a class. It is. The comments in the code is just wrong. The error you are getting on the line is because you don't have the comma.
In your code, you never break out of the switch. In a switch statement, execution falls through from case to case, so if the value of jobrole
is "3"
then your code would execute:
cash = 5.75*1.25;
cash = 5.75*1.42;
break;
So to fix this, we just return the value instead of setting a local variable. Another option would be to just break;
after each case
.
Also note, in this scenario, you don't really need to pass the value cash
in, since it is never used anyway (it is just overwritten).:
public static double rate( String jobrole )
{
//...
}
Upvotes: 1
Reputation: 41168
You need to define a method inside your class, not outside it. Move it inside the outer closing curly bracket.
There are a number of other problems in your code as well.
I recommend getting an IDE like Netbeans, they are a free download and the syntax highlighting and inline error reporting will make your life much easier. I suggest Netbeans as it is one of the most intuitive to use, you can also consider other options such as Eclipse though.
Load the file into the editor and look at each of the errors in turn. Google them and find out what they mean, then try and correct them.
Upvotes: 1