<identifier expected> error
Now I am getting another error. I am trying to have it return so that when the user enters an invalid number they can enter another number (correct number). I took away public static void main(String[] args) because the void wasn't letting it return. This is the error I get:
Code:
payRoll.java:7: error: <identifier> expected
public static boolean status(status)
^
1 error
This is the updated code:
Code:
import java.util.Scanner;
import java.util.Random;
public class payRoll
{
public static boolean status(status)
{
int payRate;
int hoursWorked;
boolean status;
Scanner keyboard = new Scanner(System.in);
{
do
{
System.out.println("Enter the hourly pay rate");
payRate = keyboard.nextInt();
}
while (payRate>7.5 && payRate<18.25);
if(payRate>7.5 && payRate<18.25)
status = true;
else
status = false;
System.out.println("You have entered an invalid number");
return status;
}
{
do
{
System.out.println("Enter the number of hours worked");
hoursWorked = keyboard.nextInt();
}
while (hoursWorked>0 && hoursWorked<40);
if(hoursWorked>0 && hoursWorked<40)
status = true;
else
status = false;
System.out.println("You have entered an invalid number");
return status;
}
}
}
Re: <identifier expected> error
You need to specify a data type for the status method.
Re: <identifier expected> error
Quote:
Originally Posted by
PhQ
You need to specify a data type for the status method.
Okay so I fixed that, I got it to compile and now I get No main methods, applets, or MIDlets found in file. I know that is because there is no main, but when I do put a main, I get so many errors. Arggggg. Any help here to get this to run???
Re: <identifier expected> error
Try this. I basically just turned the payrate variable into a double because you are using decimals.
Code:
import java.util.Scanner;
import java.util.Random;
public class payRoll
{
public static void main(String[] args)
{
double payRate;
int hoursWorked;
boolean status;
Scanner keyboard = new Scanner(System.in);
{
do
{
System.out.println("Enter the hourly pay rate");
payRate = keyboard.nextDouble();
}
while (payRate>7.5 && payRate<18.25);
if(payRate>7.5 && payRate<18.25)
status = true;
else
status = false;
System.out.println("You have entered an invalid number");
}
{
do
{
System.out.println("Enter the number of hours worked");
hoursWorked = keyboard.nextInt();
}
while (hoursWorked>0 && hoursWorked<40);
if(hoursWorked>0 && hoursWorked<40)
status = true;
else
status = false;
System.out.println("You have entered an invalid number");
}
}
}
Re: <identifier expected> error
It is good practice to inform the user if they do something wrong if you are going to repeat something until they get it right. You should insert a:
Code:
do{
System.out.println("Enter the hourly pay rate");
payRate = keyboard.nextDouble();
if(payRate<7.5 && payRate>18.25)
System.out.println("Invalid! Enter between 7.5 - 18.25 only!");
}while (payRate>7.5 && payRate<18.25);
That is just an opinion thing though.
Looking at your code, it seems like you double check each number the user enters.. If you are going to loop something until the variable is between 1-10, you don't then have to say 'if(variable is between 1-10) Do this". You have already made sure the number is between 1-10. You do this for both the payRate and the hoursWorked. It seems a bit redundant.
If you trimmed the fat off of this program, and added the extra lines I suggested, it would be about 25 lines long..
Re: <identifier expected> error
Just another FYI thing. I don't know if you wanted the:
Code:
else
status = false;
System.out.println("You have entered an invalid number");
to be all part of the else statement or not; but it is not. If there is more then one statement involved after the 'if' or the 'else', then you must use brackets. The portion "System.out.println("You have entered an invalid number");" is going to print regardless of what is true or false in the 'if' statement, as it is not going to be registered as part of the 'if' statement. It will see it as a separate piece of the code.