Java Newb
From: Travis Ray (travrichard_at_comcast.net)
Date: 01/25/04
- Next message: Raymond DeCampo: "Re: Q: Java source and directory structure - 'standard way' ?"
- Previous message: Raymond DeCampo: "Re: Running long java command from solaris shell script"
- Next in thread: Robert Tyrie: "Re: Java Newb"
- Reply: Robert Tyrie: "Re: Java Newb"
- Reply: hiwa: "Re: Java Newb"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 24 Jan 2004 21:40:43 -0500
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);
}
}
- Next message: Raymond DeCampo: "Re: Q: Java source and directory structure - 'standard way' ?"
- Previous message: Raymond DeCampo: "Re: Running long java command from solaris shell script"
- Next in thread: Robert Tyrie: "Re: Java Newb"
- Reply: Robert Tyrie: "Re: Java Newb"
- Reply: hiwa: "Re: Java Newb"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|