# Thread: How do left and right justifiers work?

1. ## How do left and right justifiers work?

For a homework assignment, this is what I need to do:

Your program should prompt the user to input the first name, last name and gross input amount,
Then output the amount of each of the deductions, the total deduction and the net pay to the screen.
Format your output to have two decimal places.
A sample input: Bill Robinson 100000
will produce the following output :

Java Code:
```Please enter employee's first name :Bill

Please enter employee's last name :Robinson

Bill Robinson

Gross Amount:                  \$ 100000.00

Federal Tax:                   \$  15000.00

State Tax:                     \$   5000.00

Medicare/Medicaid Tax:         \$   1450.00

Pension Plan:                  \$   5000.00

Health Insurance:              \$     75.00

Total Deduction:               \$  30725.00

Net Pay:                       \$  69275.00```
Now I have already typed up my program that produces everything correctly with the exception of the formatting of the dollar amounts to be aligned perfectly. I need help trying to figure out how to format that within my program so help would be super awesome!

This is my program:
Java Code:
```import java.util.*;

public class Payroll
{
static Scanner console = new Scanner(System.in);

static final double FED = 0.15;
static final double STATE = 0.05;
static final double SS = 0.042;
static final double MED = 0.0145;
static final double PEN = 0.05;
static final double HEALTH = 75.00;

public static void main(String[] args)
{

//declare variables
String first, last;
double pay;
double deduct;
double netpay;

//Statements: Step 1 - Step
System.out.print("Please enter employee's first name :");
first = console.next();
System.out.println();

System.out.print("Please enter employee's last name :");
last = console.next();
System.out.println();

pay = console.nextInt();
System.out.println();

deduct = FED*pay + STATE*pay + SS*pay + MED*pay + PEN*pay + HEALTH;

netpay = pay - deduct;

System.out.println(first + " " + last);

System.out.printf("Gross Amount:		\$%10.2f\n", pay);

System.out.printf("Federal Tax:		\$%10.2f\n", FED*pay);

System.out.printf("State Tax:		\$%10.2f\n", STATE*pay);

System.out.printf("Social Security Tax:		\$%10.2f\n", SS*pay);

System.out.printf("Medicare/Medicaid Tax:		\$%10.2f\n", MED*pay);

System.out.printf("Pension Plan:		\$%10.2f\n", PEN*pay);

System.out.printf("Health Insurance:		\$%10.2f\n", HEALTH);

System.out.printf("Total Deduction:		\$%10.2f\n", deduct);

System.out.printf("Net Pay:		\$%10.2f\n",netpay);

}
}```
I do know that using something like "%2d:" could be of use but I'm not sure exactly how to implement that into my program.

EDIT** I actually need to know how to center this which is where the "%2d: %2d" came into play!
Last edited by pbrockway2; 01-21-2012 at 06:45 AM. Reason: code tags added

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: How do left and right justifiers work?

At the moment you have a varying number of characters (including spaces) before you print the \$ sign. You could either be more careful with the spaces in each of the printf() statements. Or you could make the labels ("Net Pay:", "Total Deduction:" etc) arguments and leave the formatting string just to format. Your example suggests you want the \$ signs 37 characters in from the left. So the printf() statements would look like:

Java Code:
```System.out.printf("%-36s\$%10.2f\n", "Gross Amount:", pay);
System.out.printf("%-36s\$%10.2f\n", "Federal Tax:", FED*pay);
System.out.printf("%-36s\$%10.2f\n", "State Tax:", STATE*pay);
System.out.printf("%-36s\$%10.2f\n", "Social Security Tax:", SS*pay);
System.out.printf("%-36s\$%10.2f\n", "Medicare/Medicaid Tax:", MED*pay);
System.out.printf("%-36s\$%10.2f\n", "Pension Plan:", PEN*pay);
// etc```
It is the "%-36s" which does the formatting (- for left aligned, 36 for width, s for String). Details - more of them than you will remember, but worth reading to solve formatting problems as they arise - at the Formatter API docs.

Personally I think it makes the intention clearer if you have a variable for the format string and reuse it.

Java Code:
```String fmt = "%-36s\$%10.2f%n"; // <-- adjust to taste
System.out.printf(fmt, "Gross Amount:", pay);
System.out.printf(fmt, "Federal Tax:", FED*pay);
System.out.printf(fmt, "State Tax:", STATE*pay);
System.out.printf(fmt, "Social Security Tax:", SS*pay);
System.out.printf(fmt, "Medicare/Medicaid Tax:", MED*pay);
System.out.printf(fmt, "Pension Plan:", PEN*pay);
// etc```

3. ## Re: How do left and right justifiers work?

Thank you, pbrockway2!

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: How do left and right justifiers work?

You're welcome.

#### Posting Permissions

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