Results 1 to 4 of 4
- 02-21-2013, 06:06 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
Can Someone Show Me How To Format This Output Correctly
Ok, so basically this is my desired output. Notice how everything is aligned.

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.
- 02-21-2013, 07:22 PM #2
Re: Can Someone Show Me How To Format This Output Correctly
Please copy the full text of the error message and paste it here.it is giving me an error during runtime.If you don't understand my response, don't ignore it, ask a question.
- 02-21-2013, 07:27 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
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.
- 02-21-2013, 07:42 PM #4
Re: Can Someone Show Me How To Format This Output Correctly
That message says that d is not a format control for Double values..IllegalFormatConversionException: d != java.lang.Double
Read the API doc for the format class and find the correct formatting character for double valuesIf you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Can't get the decimal format to format correctly...
By Valerie1067 in forum New To JavaReplies: 5Last Post: 03-25-2012, 04:15 AM -
output format problem.
By jim01 in forum New To JavaReplies: 8Last Post: 04-18-2011, 09:00 AM -
Format the output
By Moustafa taha in forum New To JavaReplies: 5Last Post: 10-18-2010, 05:01 AM -
Getting My Packet Sniffer to show packets in Little Endian format
By PeggEffect in forum Java AppletsReplies: 7Last Post: 07-16-2010, 01:16 PM -
show a RTF FORMAT
By Jack in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks