I have a Java assignment due tonight, and I wrote this, but it will not compile. Can someone help me see what I'm doing wrong? Thanks!

My assignment is:
"Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount. "

Here is what I wrote:
import java.util.Scanner;
import java.text.NumberFormat;
public class payroll
{

public static void main(String[] args){

Scanner input=new Scanner(System.in);
NumberFormat moneyFormat=NumberFormat.getCurrencyInstance();
String last_name; //employee last name
String first_name; //employee first name
float rate; //rate of pay
float hours; //hours worked
float total; //hours multiplied by rate of pay
System.out.print("Enter employee's last name: ");
last_name=input.next(); //input string
System.out.print("Enter employee's first name: ");
first_name=input.next(); //input string
System.out.print("Enter employee's hourly rate of pay: $");
rate=input.nextFloat(); //input real number
System.out.print("Enter number of hours worked: ");
hours=input.nextFloat(); //input real number
total=rate*hours; //total weekly pay
System.out.printf("The weekly pay for %s", first_name); //display first name
System.out.printf(" %s", last_name); //display last name
System.out.printf(" is %s", moneyFormat.format(total));// display total
}
//end method main
} //end class Payroll

Recommended Answers

All 14 Replies

Hi dharajsmith and welcome to Daniweb :)

Your System.out.printf lines are incorrect syntax in Java.

As an aside, please put code in code tags as it makes it easier to read.

I'm not understanding what I need to do...I'm sorry. I am new at this...it's like a foreign language to me. What do I need to do to correct?

System.out.printf("The weekly pay for %s", first_name); //display first name

should read:

System.out.print("The weekly pay for " + first_name); // display first name

I'll leave you to figure out the other two printf lines...

This truly helps. Thank you so much. I'll come back and run it by you again, when I've made those corrections. :)

Is this right?

System.out.printf("The weekly pay for "+first_name); //display first name
System.out.printf(" "+last_name); //display last name
System.out.printf(" is "+moneyFormat.format(total));// display total

Not quite. Notice my print statement above is

System.out.print

NOT printf.
Otherwise your statements are good, although technically you don't need the ""+ in the second one, you can just write

System.out.print(last_name);

and you might want to make the last one a println statement rather than print (to give a carriage return at the end of your line).

It is often confusing for C++ programmers to use print and println rather than printf. But personally I think the java way of doing things is much more intuitive once you get the hang of it. Stay in there and you will be fine :)

Like this?

System.out.print("The weekly pay for "+first_name); //display first name
System.out.print(last_name); //display last name
System.out.printIn(" is "+moneyFormat.format(total));// display total

not In, ln - lowercase L followed by an n.

Otherwise perfect :)

duhhh....I told you I was new at this! I'm trying hard to learn though! I really appreciate your help. Thanks.

No problem :) You would be surprised though how many people confuse l and I in the println statement, so don't feel embarrassed. And we all started somewhere so feel free to ask as many questions as you need to learn - that's what this site is for.

All the best,
darkagn

Thank you for your help! So now, it should look like this?

System.out.print("The weekly pay for "+first_name); //display first name
System.out.print(last_name); //display last name
System.out.println(" is "+moneyFormat.format(total));// display total

Perfect :)

Whew! Do I feel better! Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.