Results 1 to 11 of 11
Thread: Some errors popped up....
- 03-01-2011, 10:26 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Some errors popped up....
Payroll.java:33: not a statement
else(hoursWorked < 0);
^
java:40: 'else' without 'if'
else(hoursWorked > 80);
^
Here is my code:
import java.util.Scanner;
public class Payroll
{
public static void main(String args[] )
{
Scanner input = new Scanner(System.in);
double hourlyRate = 0.0;
double hoursWorked = 0.0;
double pay = 0.0;
double ovtpay = 0.0;
System.out.print("Please Enter Your Name:");
String name = scannerObject.next();
System.out.print("Please Enter Number of Hours Worked This Week: ");
hoursWorked = input.nextDouble();
System.out.println("Please Enter Hourly Rate: ");
hourlyRate = input.nextDouble();
for(int counter=1; counter<=10; counter = counter + 1)
{
if(hoursWorked< 40)
pay = hoursWorked * hourlyRate;
else(hoursWorked < 0);
System.out.printf("Invalid. Please enter a number between 0 and 80");
if(hoursWorked>= 40)
ovtpay = (hoursWorked - 40) * 1.5 * hourlyRate;
pay = pay + ovtpay;
else(hoursWorked > 80);
System.out.printf("Invalid. Please enter a number between 0 and 80");
}
System.out.printf("\n's Totals for the Week: $%.2f\n, \n hours, \n overtime pay", name, pay, hours, ovtpay);
}
}
I am fairly new at Java and don't really know what these errors mean. Any help is great
Thanks,
BenLast edited by college{student}; 03-01-2011 at 10:29 PM.
- 03-01-2011, 10:38 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
else doesn't require a conditional
it should look like
If this condition is true, do this, else do this. The second part doesn't need to pass any test, it simply happens when the first condition is not met.Java Code:if(condition){} else{}
If you want to test multiple things you need
You can have as many else ifs as you want and do not need an else.Java Code:if(condition){} else if(condition){} else{}
Another tip.
When working with conditions(if/else)
They take one statement each
Remember a statement can be a single line or a blockJava Code:if(condition) statement else if(condition) statement
Get into the habit of surounding every response to a conditional with brackets, creating a statement blockJava Code://statement block { statement 1 statement 2 }
Im thinking of new things as I review your code.Java Code:if(condition){ statement 2 statement 1 } else if(condition){ statement 3 }
I dont see where you declared scannerObject, you made the scanner object have the name input so this line should useJava Code:String name = scannerObject.next();
Java Code:input.nextLine();
Also be weary of something like this with conditionals
You may be wanting it to test 1 and only one, however it will test all three, so if all 3 conditions return true it will execute code in each statement block. Make sure you understand the use of, and difference betweenJava Code:if(condition){} if(condition){} if(condition){}
andJava Code:if(condition){} else if(condition){} else{}
There are some useful things for modifying items as well, in loops you can increment much quicker with ++Java Code:if(condition){} if(condition){}
You also haveJava Code:for(int i = 0; i < 10; i++) is the same as for(int i = 0; i < 10; i = i + 1)
There is also *=, /=, -=, %=, all of these make the code a little easier to writeJava Code:int x = 5; x += 5; is the same as x = x + 5;
Last edited by sunde887; 03-01-2011 at 10:47 PM.
- 03-01-2011, 11:19 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Thanks a bunch. I knew about some of the stuff you said (x+=5, i++), but my professor had a hint posted and gave the "counter" line of code so I just copied and pasted from his problem. The other things will be very useful going forwards.
- 03-01-2011, 11:43 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Two more question have surfaced. I am getting to the point to input hourlyRate, and it won't initiate the next step. The cursor just returns down one line and that's where it stays. I also need to add all ten instances of pay into one at the end of the program, and since the for statement is their, I really have no idea how to gather those numbers into a sum.
- 03-01-2011, 11:49 PM #5
We don't read minds. Post your latest code and use code tags. Either click the code button or place [ code ] and [ /code ] (without the spaces) before and after your code.
- 03-01-2011, 11:52 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Why not prompt for all the information in a loop? This way it will ask the user for there input, perform calculations, print results, and continue doing this 10 times. Show me your most up to date code as well. Use code tags as well, surround your code with
[code ](omit the space)
[/code]
- 03-02-2011, 01:59 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Java Code:import java.util.Scanner; public class Payroll { public static void main(String args[] ) { Scanner input = new Scanner(System.in); double hourlyRate = 0.0; double hoursWorked = 0.0; double pay = 0.0; double ovtpay = 0.0; double totalPay = 0.0; double allPay = 0.0; System.out.print("Please Enter Your Name:"); String name = input.nextLine(); System.out.print("Please Enter Number of Hours Worked This Week: "); hoursWorked = input.nextDouble(); System.out.print("Please Enter Hourly Rate: "); hourlyRate = input.nextDouble(); for(int counter=1; counter<=10; counter = counter++) { if(hoursWorked< 40) {pay = hoursWorked * hourlyRate;} else if(hoursWorked>= 40) {ovtpay = (hoursWorked - 40) * 1.5 * hourlyRate; totalPay = pay + ovtpay;} else {System.out.printf("Invalid. Please enter a number between 0 and 80");} } System.out.printf("\n's totals for the Week: $%.2f\n, \n rate \n hours, $%.2f\n overtime pay, and $%.2f\n total pay.", name, pay, hourlyRate, hoursWorked, ovtpay, totalPay); System.out.printf("The total pay for all employees is $%.2f\n.", allPay); } }Last edited by college{student}; 03-02-2011 at 02:02 AM.
- 03-02-2011, 02:05 AM #8
Do not do this as it does not do what you think.Java Code:counter = counter++
What value will y have?Java Code:int x = 1; int y = x++;
- 03-02-2011, 02:25 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
yes, the increment operator can simply be used as
this increases the value of it by 1.Java Code:item++;
Try adding what I said in my previous post into your code(prompt inside the loop)
- 03-02-2011, 02:16 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Will I have to have 10 outputs, one for each name, or will I be able to somehow make an output for each name plus one for all 10 people together?
- 03-02-2011, 02:47 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
There are two approaches I can suggest, if you understand 2d arrays you can use a 10xX 2d array to store the data. However a more elegant solution may be to create a people class which can also perform all calculations as methods; then store an array of people objects. Once you have the data stored it's easy to loop through and perform the appropriate action.
Last edited by sunde887; 03-02-2011 at 02:49 PM.
Similar Threads
-
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
Help with three errors -.-
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 03-30-2010, 06:52 PM -
Getting errors
By Abbinormal in forum New To JavaReplies: 5Last Post: 01-15-2010, 06:01 AM -
Errors.
By rocky in forum New To JavaReplies: 4Last Post: 04-09-2009, 08:05 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks