Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: How do left and right justifiers work?

  1. #1
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default 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
    
    Please enter employee's pay :100000
    
    
    	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();
    		
    		System.out.print("Please enter employee's pay :");
    		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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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
    DarrylBurke and Fubarable like this.

  3. #3
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: How do left and right justifiers work?

    Thank you, pbrockway2!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How do left and right justifiers work?

    You're welcome.

Similar Threads

  1. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 02:45 PM
  2. Reversing dojo slider to make it slide from right to left rather left to right as giv
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-05-2011, 02:49 PM
  3. square moves left and down but not up or left
    By natdizzle in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2011, 06:20 PM
  4. mp5 left!!.. anyOne know it?.
    By jhon123 in forum New To Java
    Replies: 0
    Last Post: 01-31-2011, 06:50 AM
  5. Align left
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 09-09-2009, 05:05 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
  •