Question

I am having two problems with my code.
First: I can't seem to add "$" in the right location (I cant get it to look like $10.00 only 10.00$)
Second: Adding a Scanner class ends up with the program "running" but nothing happening. (if I set gross with a number it runs fine but not with using a scanner class)

import java.util.Scanner;
public class Payment
{
    public static void main(String[] args) 
    { 
        Scanner Keyboard = new Scanner(System.in);
        //double gross = Keyboard.nextDouble(); will not work
        //double gross = 8000; will work
        double fed = (0.15 * gross);
        double state = (0.035 * gross);
        double soc = (0.0575 * gross);
        double med = (0.0275 * gross);
        double pen = (0.05 * gross);
        double hea = 75;
        double net = (gross - (fed + state + soc + med + pen + hea));

        System.out.println("Paycheck calculation by employee\n");
        System.out.printf("Gross Amount:%28.2f%n", gross);
        System.out.printf("Federal Tax:%29.2f%n", fed);
        System.out.printf("State Tax:%31.2f%n", state);
        System.out.printf("Social Security Tax:%21.2f%n", soc);
        System.out.printf("Medicare/Medicaid Tax:%19.2f%n", med);
        System.out.printf("Pension Plan %28.2f%n", pen);
        System.out.printf("Health Insurance %24.2f%n%n", hea);
        System.out.printf("Net Pay:%33.2f", net);
    }
}
Was it helpful?

Solution

You probably want to print out an input prompt. Regarding currency formatting, you could use the DecimalFormat class.

import java.text.DecimalFormat;
import java.util.Scanner;
public class Payment
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter gross amount: ");
        double gross = keyboard.nextDouble();
        //double gross = 800; //will work
        double fed = (0.15 * gross);
        double state = (0.035 * gross);
        double soc = (0.0575 * gross);
        double med = (0.0275 * gross);
        double pen = (0.05 * gross);
        double hea = 75;
        double net = (gross - (fed + state + soc + med + pen + hea));
        DecimalFormat currency = new DecimalFormat("$0.00");
        System.out.println("Paycheck calculation by employee\n");
        System.out.printf("Gross Amount: %27s%n", currency.format(gross));
        System.out.printf("Federal Tax:%29s%n", currency.format(fed));
        System.out.printf("State Tax:%31s%n", currency.format(state));
        System.out.printf("Social Security Tax:%21s%n", currency.format(soc));
        System.out.printf("Medicare/Medicaid Tax:%19s%n", currency.format(med));
        System.out.printf("Pension Plan %28s%n", currency.format(pen));
        System.out.printf("Health Insurance %24s%n%n", currency.format(hea));
        System.out.printf("Net Pay:%33s", currency.format(net));
        keyboard.close();
    }
}

OTHER TIPS

Answering your first question, you could it like this:

System.out.printf("Gross Amount %28c%.2f%n", '$',  gross);

Your second question, i think your problem is the Locale. Depending on your Locale, the input format of a Double, in this case, may be different. You could do:

keyboard.useLocale(Locale.US);

This way, the input of a Double will be the integer part separated by a . from the decimal part. 8000 and 5.5 are valid examples of a Double input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top