-1

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);
    }
}
  • 2
    Regarding your first question, the code you posted doesn't print out a currency symbol at all. Post the code that's doing not quite what you want. Regarding your second, the problem is most likely that you're not typing in a number and pressing Enter. – chrylis -cautiouslyoptimistic- Feb 05 '14 at 01:04
  • For future reference, if would have been better to leave out everything after the `double gross` lines since they contribute nothing to the problem. (You could have tested this yourself by commenting them out.) Then you could have just provided the `Scanner` implementation and shown us your input and output since that was the only one with a problem. – jpmc26 Feb 05 '14 at 01:15

2 Answers2

1

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();
    }
}
neildo
  • 2,206
  • 15
  • 12
  • Okay, I guess I usually added the print statement and thus didn't know the scanner class wouldn't run without one. The DecimalFormat class also works like a charm, but is that the only way to do it? Can I not just add a line within the printf statement to do it? – user3273193 Feb 05 '14 at 01:25
0

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.

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28