Results 1 to 4 of 4
  1. #1
    tweaked88 is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Can Someone Show Me How To Format This Output Correctly

    Ok, so basically this is my desired output. Notice how everything is aligned.


    Can Someone Show Me How To Format This Output Correctly-screen-shot-2013-02-21-12.05.49-pm.png

    I am having trouble with a few things. First, follow the trail of int iEmployeeID. If I use my method formatOutput, that integer is outputted as a double with 2 decimal places. Can I use an IF test to say if the double passed though is == to employee ID, then convert and output it as a int?

    That's what I tried to do and it is giving me an error during runtime.

    :java.util.IllegalFormatConversionException: d != java.lang.Double
    at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:3999)
    at java.util.Formatter$FormatSpecifier.printInteger(F ormatter.java:2709)
    at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2661)
    at java.util.Formatter.format(Formatter.java:2433)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at testpaycheck.formatOutput(testpaycheck.java:70)
    at testpaycheck.main(testpaycheck.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >
    Second:

    Is there a way to align everything correctly? As the number of characters of the strings on the left vary (Federal Tax, State Tax) etc., the number of spaces in the printf function will have to vary. I'm not sure how to accomplish this.

    Here is my code:
    Java Code:
    import java.util.Scanner;
    
    public class paycheck {
    
      //Declare Tax Rates 
      public static final double FEDERAL = 0.15;
      public static final double STATE = 0.05;
      public static final double SOCIAL_SEC = 0.05;
      public static final double MEDICARE = 0.03;
      public static final double PENSION = 0.08;
      public static final double HEALTH_INSUR = 125.00;
      
     //Declare Global Variables
      static int iEmployeeID;
      static String sEmployeeFName, sEmployeeLName;
      static double dEarnings, dFedTax, dStateTax, dSocialSec, dMedicare, dPension;
      
      //Begin Main
      public static void main (String[]args) {
        
        Scanner input = new Scanner(System.in);
       
        
        //Get User Input
        
        System.out.println("Enter your employee ID: ");
        iEmployeeID = input.nextInt();
        
        System.out.println("Enter your first name: ");
        sEmployeeFName = input.next();
        System.out.println();
        
        System.out.println("Enter your last name: ");
        sEmployeeLName = input.next();
        System.out.println();
        
        System.out.println("Enter your earnings for the prior month: ");
        dEarnings = input.nextDouble();
      
        //Calc Taxes    
        dFedTax = FEDERAL * dEarnings;
        dStateTax = STATE * dEarnings;
        dSocialSec = SOCIAL_SEC * dEarnings;
        dMedicare = MEDICARE * dEarnings;
        dPension = PENSION * dEarnings; 
        
        //Output
        formatOutput("Employee ID:", iEmployeeID);
        System.out.println("Employee Name:            " + sEmployeeFName + " " + sEmployeeLName);
        printHash();
        formatOutput("EARNINGS:", dEarnings);
        formatOutput("Federal Tax:", dFedTax);
        
        
      //End Main  
      }
    
      //Print Hash Marks
      public static void printHash(){
        
        System.out.println("======================================");
       
        //End Hash Marks
      }
      
      //Format Output
      public static void formatOutput(String a, double b){
        
        if (b == iEmployeeID)
           System.out.printf(a + "%26d\n", b);
        else
           System.out.printf(a + "%26.2f\n", b);
     
      }
      
      //Calculate Net Pay
      public static double netPay(double dNetPay){
    
        dNetPay = dEarnings - (dFedTax + dStateTax + dSocialSec + dMedicare + dPension + HEALTH_INSUR);
        
       return dNetPay; 
       //End Calc Net pay
      }
      
      
      
    }
    Last edited by tweaked88; 02-21-2013 at 07:27 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Can Someone Show Me How To Format This Output Correctly

    it is giving me an error during runtime.
    Please copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tweaked88 is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Can Someone Show Me How To Format This Output Correctly

    There you go. Any insight?

    What I'm primarily worried about is how to right-align the numeric values. As you can see the spacing changes when the string to the left of it is shorter or longer.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Can Someone Show Me How To Format This Output Correctly

    .IllegalFormatConversionException: d != java.lang.Double
    That message says that d is not a format control for Double values.

    Read the API doc for the format class and find the correct formatting character for double values
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Can't get the decimal format to format correctly...
    By Valerie1067 in forum New To Java
    Replies: 5
    Last Post: 03-25-2012, 04:15 AM
  2. output format problem.
    By jim01 in forum New To Java
    Replies: 8
    Last Post: 04-18-2011, 09:00 AM
  3. Format the output
    By Moustafa taha in forum New To Java
    Replies: 5
    Last Post: 10-18-2010, 05:01 AM
  4. Replies: 7
    Last Post: 07-16-2010, 01:16 PM
  5. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 03:37 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
  •