Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Formatting using Printf()

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Formatting using Printf()

     
    //import Scanner
    import java.util.Scanner;
     
    public class Program3{
     
         public static void main(String[] args) {
     
             //intiate the Scanner
             Scanner input; 
             input = new Scanner(System.in);
     
             //declare loan amount
             System.out.print("Enter Loan Amount: ");
             double loanAmount = input.nextDouble();
     
             //declare int NumOfYears
             System.out.print("Number Of Years: ");
             int NumOfYears = input.nextInt();
     
     
     
             //declare annual interest rate
             double AnnualInterestRate = 5;  // intital
     
              System.out.println("Interest Rate: \t Monthly Rate:\t    Total Payment: \t");
            //intial loop
        while (AnnualInterestRate <= 8) //test
        {
             //Monthly
             double MonthlyInterestRate = AnnualInterestRate/1200;
     
             //Calculate monthly payment
             double monthlyPayment = loanAmount * MonthlyInterestRate / (1 - (Math.pow(1 / (1 + MonthlyInterestRate), NumOfYears * 12)));
     
           //calculate total payment
             double totalPayment = monthlyPayment * 12 * NumOfYears;
     
             //ouput of total, monthly, annual
             System.out.print(AnnualInterestRate + ("          \t") + monthlyPayment + "   \t" + totalPayment);
             System.out.printf("%8.2f", AnnualInterestRate , "8.2f" , monthlyPayment, "%8.2f", totalPayment );
             System.out.println();
    // change
     
             AnnualInterestRate += .125;
     
         } //while
         }//main
    }// class

    I'm really not sure what I'm doing wrong, I've read up on the printf() and formatting but just can't seem to get this to format right. It needs to come out to two decimal places and should stay in a 3 column output. Please any suggestions?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Formatting using Printf()

    The printf() method takes a String followed by the values that replace the format Specs IN the string:

    System.out.printf( "%8.2f %8.2f %8.2f", AnnualInterestRate, monthlyPayment, totalPayment );

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Formatting using Printf()

    Thank you man.

    --- Update ---

    Thank you man

Similar Threads

  1. Help with Printf statement
    By richardman54 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 23rd, 2013, 03:28 PM
  2. Replies: 3
    Last Post: September 18th, 2013, 02:40 AM
  3. Java printf
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 27th, 2013, 03:20 PM
  4. [SOLVED] printf error
    By Thor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 10:05 PM
  5. about printf please help having errors
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 11:01 PM