3

I am very new. apologies in advance for my coding. I need to print a table that shows year and then a tab over, and then the value with a next line. The value has to be in decimal form. I have been reading and searching and mixing my code around. I have found it for 1 variable but not for two in same line. I have tried the printf, I have tried the good ol 100 / 100.0 and I either get errors or the decimal never goes to 2 places. I do not need it rounded, just with 2 spaces after. I am obviously going wrong somewhere. I would appreciate any assistance. import java.util.Scanner;

public class Investment1 {

public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){

double principal = 0.0;
double futureInvestmentValue = 0;
for (years = 1; years <=30; years++){
    //calculate futureInvestmentValue
    futureInvestmentValue = (investmentAmount * (Math.pow (1 + monthlyInterestRate, years * 12)));
    System.out.print(years + "\t" + futureInvestmentValue + "\n");

}//end for
    return futureInvestmentValue;
}//end futureInvestmentValue

public static void main(String[] args){

    Scanner input = new Scanner (System.in);

    //obtain Investment amount
    System.out.print("Enter Investment amount: ");
    double investmentAmount = input.nextDouble();

    //obtain monthly interest rate in percentage
    System.out.print("Enter annual interest rate in percentage: ");
    double annualInterestRate = input.nextDouble();
    double monthlyInterestRate = (annualInterestRate / 1200);

int years = 30;

System.out.println("Years\t" + "Future Value");
System.out.print(years + "\t");
System.out.print(years + "\t" + ((int)(futureInvestmentValue(investmentAmount, monthlyInterestRate, years))) + "\n");

}//end main
}//end Investment
Lish
  • 231
  • 4
  • 6
  • 14
  • What errors do you get? +1 for clear effort. – christopher Feb 26 '13 at 16:36
  • @ChrisCooney - with the above code (minus the extra print years line that I just took out.) no errors, just not going to decimal place. However, when I try to put sop"years + "\t" + "%.2f", future... The error code is no suitable method found for print. I then tried the string that was suggested below and the error codes went on and on. – Lish Feb 26 '13 at 16:52
  • @ChrisCooney, if I do the * 100 / 100.0 no error. It just doesn't decimal to 2 – Lish Feb 26 '13 at 16:54
  • What's the exact output with the 100/100.0 ? – christopher Feb 26 '13 at 16:54
  • @ChrisCooney it shows table just as needed - with years in one column and value as i.e. 1093.8068976709837 – Lish Feb 26 '13 at 17:00
  • 1
    Okay. One solution that springs to mind is to get x = indexof the decimal place, and then substring 0 to x + 2? – christopher Feb 26 '13 at 17:02
  • @ChrisCooney perhaps I am putting the math in the wrong spot. I put it under the double future value = (1 * 100)/100.0 If I put it in the print out area same way, its telling me that it is not a statement. – Lish Feb 26 '13 at 17:02

2 Answers2

1

You can use system.out.format():

System.out.format("%d \t %.2f", years, futureInvestmentValue);
P.P
  • 117,907
  • 20
  • 175
  • 238
0

you should read about format strings, heres a simple usage example:

System.out.println(String.format("%d %.2f",myint,myfloat));

myint will be printed as an integer (even if it's a floating point value) due to the use of the %d in the format string.
myfloat will be printed as a decimal number with 2 digits after the decimal point, thanks to the %f.2 part in the format string.

yurib
  • 8,043
  • 3
  • 30
  • 55
  • I will definitely read more on that. Question for you though. how do you put the tab and next line into that? would you do i,e. "%d\t %.2d\n" ? – Lish Feb 26 '13 at 17:05
  • 1
    yep, only notice that you used %.2d instead of %.2f – yurib Feb 26 '13 at 17:11