Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default 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,
    Ben
    Last edited by college{student}; 03-01-2011 at 10:29 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    else doesn't require a conditional

    it should look like
    Java Code:
    if(condition){}
    else{}
    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.

    If you want to test multiple things you need
    Java Code:
    if(condition){}
    else if(condition){}
    else{}
    You can have as many else ifs as you want and do not need an else.

    Another tip.

    When working with conditions(if/else)
    They take one statement each
    Java Code:
    if(condition)
      statement
    else if(condition)
      statement
    Remember a statement can be a single line or a block
    Java Code:
    //statement block
    {
    statement 1
    statement 2
    }
    Get into the habit of surounding every response to a conditional with brackets, creating a statement block
    Java Code:
    if(condition){
     statement 2
     statement 1
    }
    else if(condition){
     statement 3
    }
    Im thinking of new things as I review your code.
    Java Code:
    String name = scannerObject.next();
    I dont see where you declared scannerObject, you made the scanner object have the name input so this line should use
    Java Code:
    input.nextLine();

    Also be weary of something like this with conditionals
    Java Code:
    if(condition){}
    if(condition){}
    if(condition){}
    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 between
    Java Code:
    if(condition){}
    else if(condition){}
    else{}
    and
    Java Code:
    if(condition){}
    if(condition){}
    There are some useful things for modifying items as well, in loops you can increment much quicker with ++
    Java Code:
    for(int i = 0; i < 10; i++)
    
    is the same as
    
    for(int i = 0; i < 10; i = i + 1)
    You also have
    Java Code:
    int x = 5;
    x += 5;
    
    is the same as 
    
    x = x + 5;
    There is also *=, /=, -=, %=, all of these make the code a little easier to write
    Last edited by sunde887; 03-01-2011 at 10:47 PM.

  3. #3
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  4. #4
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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]

  7. #7
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Java Code:
    counter = counter++
    Do not do this as it does not do what you think.

    Java Code:
    int x = 1;
    int y = x++;
    What value will y have?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    yes, the increment operator can simply be used as
    Java Code:
    item++;
    this increases the value of it by 1.

    Try adding what I said in my previous post into your code(prompt inside the loop)

  10. #10
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  1. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  2. Help with three errors -.-
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 03-30-2010, 06:52 PM
  3. Getting errors
    By Abbinormal in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 06:01 AM
  4. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 08:05 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •