Search code examples
javaprintfright-align

Adding "$" to right aligned numbers and Scanner class not running, java


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);
    }
}

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();
        }
    }