import java.text.DecimalFormat;
import java.util.Scanner;

public class Main
{
    public static void main (String[] args)
    {
        String inputString;      //For reading input.
        String input;            //Read Input
        double automobileCost;
        double warrantyCost;
        double downPaymentAmount;
        double interestRate;
        double numberPayments;
        double salesTaxAmount;
        double totalPurchaseCost;
        double amountFinanced;
        double loanInterestRate;
        double loanLength;
        double monthlyPayment;
        double annualInterestRate;

        // Create a Scanner object to read input.
        Scanner keyboard = new Scanner(System.in);

        //Create a DecimalFormat object
        DecimalFormat formatter = new DecimalFormat("#0.00");

        //Get the heading of the report
        System.out.println("                    Deals on Wheels!");
        System.out.println("Automobile Loan Schedule -- prepared for"
              + " John Smith \n ");

        // Get the Automobile price.
        System.out.print("Automobile price:             $ ");
        automobileCost = keyboard.nextDouble();

        //Get the Extended warranty.
        System.out.print("Extended warranty:            $");
        warrantyCost = keyboard.nextDouble();

        //Get the Sales tax.
        System.out.print("Sales tax:                    $");
        salesTaxAmount = .0625 * automobileCost;
        System.out.println(formatter.format(salesTaxAmount));
        System.out.print("                              ________\n ");

        //Get the Total cost.
        System.out.print("totalPurchaseCost:           $");{
        totalPurchaseCost = automobileCost + warrantyCost + salesTaxAmount;
        System.out.println(formatter.format(totalPurchaseCost));


        //Get the Down payment.
        System.out.print("Down payment:                 $");
        downPaymentAmount = keyboard.nextDouble();

        //Get the Amount Financed
        System.out.print("Amt. Financed:                $");
        amountFinanced = totalPurchaseCost - downPaymentAmount;
        System.out.println(formatter.format(amountFinanced));
        System.out.print("                                ________\n ");

        //Get the Annual interest rate.
        System.out.println("Annual interest rate:    ");
        annualInterestRate = keyboard.nextDouble();

        //Get the length of the loan
        System.out.println("Length of loan:      months");
        loanLength = keyboard.nextDouble();

        //Get the monthy payment.
        System.out.println("Monthly Payment        $");{
        monthlyPayment = (amountFinanced * annualInterestRate/12) /
                (1 - (Math.pow(1 + annualInterestRate/12, -loanLength )));
        System.out.println(formatter.format(monthlyPayment));
        }

       

         System.exit(0);
    }
 }

How do I align each numbers properly and each category names properly? Please help me by showing me how to add it on my original program

Recommended Answers

All 10 Replies

There is no way to align System.out.print's. You will just have to use the \t,\n etc..to achieve your desired result.
e.g.

System.out.print("Name\tAge\tAddress\n");
System.out.println("James\t20\tSandyHouse");

There is no way to align System.out.print's. You will just have to use the \t,\n etc..to achieve your desired result.
e.g.

System.out.print("Name\tAge\tAddress\n");
System.out.println("James\t20\tSandyHouse");

can you show me an example (from my program) on where I should insert it...sorry im a newbie at this

I'm not on a pc with java installed so unfortunately I cannot run your program and look at the layout. But, just looking at your code, you use white space for layout.

System.out.println("Extended warranty:         $")
//anywhere you use the same white space for layout, change these to
System.out.println("Extended warranty:\t$")

And see what happens.

I'm not on a pc with java installed so unfortunately I cannot run your program and look at the layout.

oh ok....ive spent about 3 hrs just trying to figure out how to align this thing...and ive had no success at all

re-read my above post.

Sorry, you will then ofcourse line up your values using the \t also.

System.out.println("\t"+formatter.format(salesTaxAmount));

sorry it still doesnt align properly :(

e.g.

DecimalFormat formatter = new DecimalFormat("#0.00");
      System.out.println(" Deals on Wheels!");
      System.out.println("Automobile Loan Schedule -- prepared for"
      + " John Smith \n ");
      System.out.print("Automobile price:\t$");
      automobileCost = keyboard.nextDouble();
      System.out.print("Extended warranty:\t");
      warrantyCost = keyboard.nextDouble();
      System.out.print("Sales tax:\t\t");
      salesTaxAmount = .0625 * automobileCost;
      System.out.println(formatter.format(salesTaxAmount));
      System.out.print("\t\t\t ________\n ");

You will never get everything to line up exactly unless you spend hours putting the right amount of white space mixed with \t, \n etc. It is an entirely pointless excersise

To get it to line up like your image, you'll need to use the c-style printf() function. Since this is well documented by Sun, I'll just briefly review it.

printf is based on the function of that name in the C language. It allows in-line interpolation of variables with some formatting capabilities. A variable is interpolated by including a % sign followed by the appropriate character, ie %d for a decimal integer, %s for a string, %f for a floating point number (float or double). Numbers are formatted by including a digit representing the number of spaces desired for the representation. Floating point numbers are formatted by a digit.digit specifier, where the first digit is the total number of spaces and the second is the number of spaces following the decimal. These interpolations reserve spaces for Strings, ints, doubles, and so forth. The actual values to use are listed following the format string, separated by commas.

Newlines and tabs are inserted as with the ordinary print or println methods, as \n or \t.

Example:

String foo = "Foo! Foo! Foo!";
int i = 2;
int j = 3;
double d = 0.66;
System.out.printf("Foo = %s \t i =%d \t j =%5d \t d=%3.2f i/j=%5.3f", foo, i, j, d, (double)i/j);

Produces
Foo = Foo! Foo! Foo! i =2 j = 3 d=0.66 i/j=0.667

(tabs were eaten by Daniweb's display... oh, well, run it yourself to see them)

I hope this helps. If you need more, you might find this to be a useful resource. There are plenty of other articles found by searching on "java printf", as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.