By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Java Newb

P: n/a
Hi, I just started a java class and I have this assignment and I'm
stumped... I realize the code is messy... but I just started... so go easy
on me :)

Java Problem
I am working on this assignment. The user enters a loan amount, number of
years to repay, and annual percentage rate. The project is supposed to
output a table for each month's payment information. I wrote the attached
code and it works well. However, there is a lot of extra code to try and
get around a formatting problem. The output prints out in columns... when
an amount is something like 32.50 it prints out as 32.5 and messes up my
columns... very fustrating. As you will see in the code, I have attempted
to take steps to prevent this. It works 95% of the time... however... it
sometimes doesn't add the 0... and it sometimes addes a zero to the end of a
number that should not have a zero at the end... for example... it
occassionally turns something like 29.65 into 29.650. this screws up my
columns further. Is there a way to force 2 decimal places in a java
program? Or is there a way to align columns other than putting spacer
strings inbetween data elements?

import javax.swing.JOptionPane;

public class Loan {
public static void main(String[] args) {
double annualInterestRate;
int numOfYears = 0;
double loanAmount = 0;
double monthlyPayment = 0;
double interest = 0;
double principal = 0;
double principalpay = 0;
double payments = 0;
String spacer1="";
String spacer2="";
String spacer3="";

String loanAmountString = JOptionPane.showInputDialog(null,
"Enter Loan Amount:","Loan Amount",JOptionPane.QUESTION_MESSAGE);

loanAmount=
Double.parseDouble(loanAmountString);

String numOfYearsString = JOptionPane.showInputDialog(null,
"Enter Number of Years:","Number of Years",
JOptionPane.QUESTION_MESSAGE);

numOfYears = Integer.parseInt(numOfYearsString);

String annualInterestRateString = JOptionPane.showInputDialog(null,
"Enter Annual Interest Rate:","Annual Interest Rate",
JOptionPane.QUESTION_MESSAGE);

annualInterestRate =
Double.parseDouble(annualInterestRateString);

principal=loanAmount;
payments=numOfYears*12;
int mypayments=(int)payments;

String[][] TableList;
TableList=new String[mypayments+1][4];

TableList[0][0] = "Payment#";
TableList[0][1] = "Interest";
TableList[0][2] = "Principal";
TableList[0][3] = "Balance";

for(int i=1;i < mypayments+1; i++)
{
TableList[i][0]=String.valueOf(i);
}
String output=TableList[0][0] + " " + TableList[0][1] + " " +
TableList[0][2] + " " + TableList[0][3];

for(int i=1; i < mypayments+1; i++)
{
interest = (principal*(annualInterestRate/100))/12;
monthlyPayment = principal*((annualInterestRate/100)/12)/
(1-(Math.pow(1/(1+((annualInterestRate/100)/12)), payments)));
principalpay = monthlyPayment - interest;
principal = principal - principalpay;
monthlyPayment = (int) (monthlyPayment * 100) / 100.0;
double myinterest = (int)(interest * 100) / 100.0;
double myprincipal = (int)(principal * 100) / 100.0;
double myprincipalpay = (int)(principalpay * 100) / 100.0;
int testint=(int)(myinterest*100);
int testprin=(int)(myprincipal*100);
int testpay=(int)(myprincipalpay*100);

TableList[i][1]=String.valueOf(myinterest);
TableList[i][2]=String.valueOf(myprincipalpay);
TableList[i][3]=String.valueOf(myprincipal);

if(testint % 10 == 0)
TableList[i][1]=TableList[i][1]+'0';
if(testprin % 10 == 0)
TableList[i][3]=TableList[i][3]+'0';
if(testpay % 10 == 0)
TableList[i][2]=TableList[i][2]+'0';
payments--;

spacer1=".........";
spacer2="......";
spacer3="......";

if(i > 9)
spacer1="........";
if(i > 99)
spacer1=".......";
if(interest>99.99)
spacer2=".....";
if(interest<10)
spacer2=".......";
if(principalpay<10)
spacer3="......";

output = output + "\n\r" + TableList[i][0] +
spacer1 + TableList[i][1] +
spacer2 + TableList[i][2] +
spacer3 + TableList[i][3];
}
System.out.println(output);


System.exit(0);
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <tr*********@comcast.net>
wrote:
Or is there a way to align columns other than putting spacer
strings inbetween data elements?


Take a look at the DecimalFormat class,

http://java.sun.com/j2se/1.4.2/docs/...malFormat.html
Jul 17 '05 #2

P: n/a
On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <tr*********@comcast.net>
wrote:
Or is there a way to align columns other than putting spacer
strings inbetween data elements?


Take a look at the DecimalFormat class,

http://java.sun.com/j2se/1.4.2/docs/...malFormat.html
Jul 17 '05 #3

P: n/a
It worked after a long time of studying the NumberFormat and Locale classes.
Thank You :)

"Robert Tyrie" <ro******@yahoo.ca> wrote in message
news:op**************@news.cogeco.ca...
On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <tr*********@comcast.net>
wrote:
Or is there a way to align columns other than putting spacer
strings inbetween data elements?


Take a look at the DecimalFormat class,

http://java.sun.com/j2se/1.4.2/docs/...malFormat.html

Jul 17 '05 #4

P: n/a
It worked after a long time of studying the NumberFormat and Locale classes.
Thank You :)

"Robert Tyrie" <ro******@yahoo.ca> wrote in message
news:op**************@news.cogeco.ca...
On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <tr*********@comcast.net>
wrote:
Or is there a way to align columns other than putting spacer
strings inbetween data elements?


Take a look at the DecimalFormat class,

http://java.sun.com/j2se/1.4.2/docs/...malFormat.html

Jul 17 '05 #5

P: n/a
If your problem is text formatting for number strings, you should use
java.text.Format and its derivatives -- especially DecimalFormat.
Jul 17 '05 #6

P: n/a
If your problem is text formatting for number strings, you should use
java.text.Format and its derivatives -- especially DecimalFormat.
Jul 17 '05 #7

P: n/a

"Robert Tyrie" <ro******@yahoo.ca> wrote in message
news:op**************@news.cogeco.ca...
On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <tr*********@comcast.net>
wrote:
Or is there a way to align columns other than putting spacer
strings inbetween data elements?


Take a look at the DecimalFormat class,

http://java.sun.com/j2se/1.4.2/docs/...malFormat.html


As an *exercise*, might also want to do the formatting manually?
.... by using String .indexOf('.') and .length() methods to determine how
many charaters after the decimal point, then add the append the appropriate
number of zeros to give you two decimal places. For alligning columns,
probably need to use .length() on individual strings to determine how many
spaces to use in padding to get column alignment.
Jul 17 '05 #8

P: n/a

"Robert Tyrie" <ro******@yahoo.ca> wrote in message
news:op**************@news.cogeco.ca...
On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <tr*********@comcast.net>
wrote:
Or is there a way to align columns other than putting spacer
strings inbetween data elements?


Take a look at the DecimalFormat class,

http://java.sun.com/j2se/1.4.2/docs/...malFormat.html


As an *exercise*, might also want to do the formatting manually?
.... by using String .indexOf('.') and .length() methods to determine how
many charaters after the decimal point, then add the append the appropriate
number of zeros to give you two decimal places. For alligning columns,
probably need to use .length() on individual strings to determine how many
spaces to use in padding to get column alignment.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.