Join 150,048 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,694 people online right now. Registration is fast and FREE... Join Now!
I have this code, for a java course. I was informed my first 2 loans -the numbers come out wrong. And I was told to put this an a ARRAY.
Any assistance would be appreciated.
CODE
import java.text.*; import java.io.IOException;
class mortgagecalc //program class name { public static void main(String[] args) throws IOException { double amount,moPay,totalInt,principal = 200000; //monthly payment, total interest and principal double rate [ ] = {.0535, .055, .0575}; //the three interest rates int [ ] term = {7,15,30}; //7, 15 and 30 year term loans int time,ratePlace = 0,i,pmt=1,firstIterate = 0,secondIterate = 0,totalMo=0; char answer,test; boolean validChoice; System.in.skip(System.in.available()); //clear the stream for the next entered character do { validChoice = true; System.out.println("What Choice would you Like\n"); System.out.println("1-Interest rate of 5.35% for 7 years?"); //just as it reads for each to select from System.out.println("2-Interest rate of 5.55% for 15 years?"); System.out.println("3-Interest rate of 5.75% for 30 years?"); System.out.println("4-Quit");
answer = (char)System.in.read();
if (answer == '1') //7 yr loan at 5.35% { ratePlace = 0; firstIterate = 4; secondIterate = 21; totalMo= 84; } else if (answer == '2') //15 yr loan at 5.55% { ratePlace = 1; firstIterate = 8; secondIterate = 24; totalMo = 180; } else if (answer == '3') //30 yr loan at 5.75% { ratePlace = 2; firstIterate = 15; secondIterate = 24; totalMo = 360; } else if (answer == '4') //exit or quit { System.exit(0); } else { System.in.skip(System.in.available()); //validates choice System.out.println("Invalid choice, try again.\n\n"); validChoice = false; } }while(!validChoice); //when a valid choice is found calculate the following, ! means not, similar to if(x != 4)
for(int x = 0; x < 25; x++)System.out.println(); amount = (principal + (principal * rate[ratePlace] * term[ratePlace] )); moPay = (amount / totalMo); totalInt = (principal * rate[ratePlace] * term[ratePlace]); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("The Interest on $" + (df.format(principal) + " at " + rate[ratePlace]*100 + "% for a term of "+term[ratePlace]+" years is \n" +"$"+ (df.format(totalInt) +" Dollars\n"))); System.out.println("\nThe total amount of loan plus interest is $"+(df.format(amount)+" Dollars.\n")); System.out.println("\nThe Payments spread over "+term[ratePlace]* 12+" months would be $"+ (df.format (moPay) + " Dollars a month\n\n")); System.in.skip(System.in.available()); System.out.println("\nWould you like to see a planned payment schedule? y for Yes, or x to Exit"); answer = (char)System.in.read(); if (answer == 'y') { System.out.println((term[ratePlace]*12) + " monthly payments:"); String monthlyPayment = df.format(moPay); for (int a = 1; a <= firstIterate; a++) { System.out.println(" Payment Schedule \n\n "); System.out.println("Month Payment Balance\n"); for (int b = 1; b <= secondIterate; b++) { amount -= Double.parseDouble(monthlyPayment); System.out.println(""+ pmt++ +"\t"+ monthlyPayment + "\t"+df.format(amount)); } System.in.skip(System.in.available()); System.out.println(("This Page Is Complete Press [ENTER] to Continue")); System.in.read(); } } } }
ok I fixed my 15 year loan portion. how would I present all 3 loans to the user in an array for viewing
as quoted by my instructor "Week 4 Great work, the numbers were a little off on the 1st and 2nd loans. Next time try putting the values you displayed in an array. "
CODE
import java.text.*; //20 May 2008, Revision ...many import java.io.IOException;
class memortgage //program class name { public static void main(String[] args) throws IOException { double amount,moPay,totalInt,principal = 200000; //monthly payment, total interest and principal double rate [ ] = {.0535, .055, .0575}; //the three interest rates int [ ] term = {7,15,30}; //7, 15 and 30 year term loans int time,ratePlace = 0,i,pmt=1,firstIterate = 0,secondIterate = 0,totalMo=0; char answer,test; boolean validChoice; System.in.skip(System.in.available()); //clear the stream for the next entered character do { validChoice = true; System.out.println("What Choice would you Like\n"); System.out.println("1-Interest rate of 5.35% for 7 years?"); //just as it reads for each to select from System.out.println("2-Interest rate of 5.55% for 15 years?"); System.out.println("3-Interest rate of 5.75% for 30 years?"); System.out.println("4-Quit");
answer = (char)System.in.read();
if (answer == '1') //7 yr loan at 5.35% { ratePlace = 0; firstIterate = 4; secondIterate = 21; totalMo= 84; } else if (answer == '2') //15 yr loan at 5.55% { ratePlace = 1; firstIterate = 9; //it helps If I have the correct numbers to calcualte as in 9*20 to equal 180...that why I was off -24333.76 secondIterate = 20; //now it is only of -0.40 cents....DAMN ME totalMo = 180; } else if (answer == '3') //30 yr loan at 5.75% { ratePlace = 2; firstIterate = 15; secondIterate = 24; totalMo = 360; } else if (answer == '4') //exit or quit { System.exit(0); } else { System.in.skip(System.in.available()); //validates choice System.out.println("Invalid choice, try again.\n\n"); validChoice = false; } }while(!validChoice); //when a valid choice is found calculate the following, ! means not, similar to if(x != 4)
for(int x = 0; x < 25; x++)System.out.println(); amount = (principal + (principal * rate[ratePlace] * term[ratePlace] )); moPay = (amount / totalMo); totalInt = (principal * rate[ratePlace] * term[ratePlace]); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("The Interest on $" + (df.format(principal) + " at " + rate[ratePlace]*100 + "% for a term of "+term[ratePlace]+" years is \n" +"$"+ (df.format(totalInt) +" Dollars\n"))); System.out.println("\nThe total amount of loan plus interest is $"+(df.format(amount)+" Dollars.\n")); System.out.println("\nThe Payments spread over "+term[ratePlace]* 12+" months would be $"+ (df.format (moPay) + " Dollars a month\n\n")); System.in.skip(System.in.available()); System.out.println("\nWould you like to see a planned payment schedule? y for Yes, or x to Exit"); answer = (char)System.in.read(); if (answer == 'y') { System.out.println((term[ratePlace]*12) + " monthly payments:"); String monthlyPayment = df.format(moPay); for (int a = 1; a <= firstIterate; a++) { System.out.println(" Payment Schedule \n\n "); System.out.println("Month Payment Balance\n"); for (int b = 1; b <= secondIterate; b++) { amount -= Double.parseDouble(monthlyPayment); System.out.println(""+ pmt++ +"\t"+ monthlyPayment + "\t"+df.format(amount)); } System.in.skip(System.in.available()); System.out.println(("This Page Is Complete Press [ENTER] to Continue")); System.in.read(); } } } }
I have one day to figure this out,...I would not be asking for help if I honestly did not need it.
I like to figure stuff out ....but a java course in 5 weeks...at one night a week,...is crazy for a college course for a working adult
I have read many of readings including school. They all do say how to make arrays....but not for a mortgage. what gets me is my course does not even explain mortgages....just items such as what I am asking "ARRAYS" I got this far...I just do not now how to do my mortgage code in a java array for the values to be shown to the user ....
I do understand array as in this...( which is one array shown to us for about 5 minutes)
CODE
import java.io.*;
public class TestScore{ static BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
public static void main (String[] args) throws IOException{
String [] Score = new String[11]; String s1,s2,s3; int ans = 1;
//Read Array for (int n = 0;n<10; n++){ System.out.println("Please enter a test score"); s1 = in.readLine(); Score[n]=s1; }
//Display Entire Array for (int j=0; j<10; j++){ System.out.println("Test No. " + (j+1) + " is " + Score[j]); }
//Display test score as requested by user while (ans==1) {
System.out.println ("What test score number would you like; "); s2=in.readLine(); int k = Integer.parseInt(s2); System.out.println("Test # " + k + " is " + Score[k-1]); System.out.println ("Would you like to ask again?(1 for yes)"); s3=in.readLine(); ans = Integer.parseInt(s3); }
} }
This post has been edited by mysong: 3 Jun, 2008 - 07:13 PM
What does your teacher want into an array ? Something like that:
CODE
System.out.println("What Choice would you Like\n"); for(int i = 0; i < 3; i++) System.out.println((i+1) + "-Interest rate of " + (rate[i] * 100) + "% for " + term[i] + " years ?"); System.out.println("4-Quit");
instead of
CODE
System.out.println("What Choice would you Like\n"); System.out.println("1-Interest rate of 5.35% for 7 years?"); //just as it reads for each to select from System.out.println("2-Interest rate of 5.55% for 15 years?"); System.out.println("3-Interest rate of 5.75% for 30 years?"); System.out.println("4-Quit");
do { validChoice = true; System.out.println("What Choice would you Like\n"); for(int i = 0; i < 3; i++) System.out.println((i+1) + "-Interest rate of " + (rate[i] * 100) + "% for " + term[i] + " years ?"); System.out.println("4-Quit");
wow you are good and know this stuff.. I see what you mean...and I like it...I just have to find where to edit mine ( I can see it ) but I am affraid I will mess up my code that I will still need. Trust me I am trying items out......I have about 45 *.java files now from testing them all and compiling them with errors
the values that the users will see on the screen...is what he wants in an array, as the outcome
Really stupid.... or you must have misunderstood him The use of arrays just cut your code by a few dozens lines and they are usefull Actually if you had used them at the beginning you wouldn't have had you calculations problem
Now you are using a loop to display your result
CODE
for(int x = 0; x < 25; x++) { do some calculation display results }
you can store the results of your calculation in defferents arrays
CODE
double[] totalInt = new double[25]; double[] moPay = new double[25]; .... for(int x = 0; x < 25; x++) { amount[x] = (principal + (principal * rate[ratePlace] * term[ratePlace] )); moPay[x] = (amount[x] / totalMo); totalInt[x] = .... }
and then you will need another loop to display the results...
for(int x = 0; x < 25; x++) { System.out.println(....... amount[x]..... moPay]x]... }
wouldn't be that bad but you have 4 values to set up so... by making ratePlace = answer - '1'; I make ratePlace == to 0, 1, 2 then you can set your 3 others values accordingly
I am inputting your suggestions,..but I am getting errors.
CODE
import java.text.*; //20 May 2008, Revision ...many import java.io.IOException;
class memortgageweek5trye //program class name { public static void main(String[] args) throws IOException { double amount,moPay,totalInt,principal = 200000; //monthly payment, total interest and principal double rate [ ] = {.0535, .055, .0575}; //the three interest rates int [ ] term = {7,15,30}; //7, 15 and 30 year term loans int time,ratePlace = 0,i,pmt=1,firstIterate = 0,secondIterate = 0,totalMo=0; char answer,test; boolean validChoice; System.in.skip(System.in.available()); //clear the stream for the next entered character do { validChoice = true; System.out.println("What Choice would you Like\n"); for(int i = 0; i < 3; i++) System.out.println((i+1) + "-Interest rate of " + (rate[i] * 100) + "% for " + term[i] + " years ?"); System.out.println("4-Quit");
System.out.println("What Choice would you Like\n"); for(int i = 0; i < 3; i++) System.out.println((i+1) + "-Interest rate of " + (rate[i] * 100) + "% for " + term[i] + " years ?"); System.out.println("4-Quit"); int[] firstIterateValues = {4, 8, 15}; int[] secondIterateValues = {21, 15, 24}; int[] totalMoValues = {84, 180, 360};
for(int x = 0; x < 25; x++)System.out.println(); amount = (principal + (principal * rate[ratePlace] * term[ratePlace] )); moPay = (amount / totalMo); totalInt = (principal * rate[ratePlace] * term[ratePlace]); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("The Interest on $" + (df.format(principal) + " at " + rate[ratePlace]*100 + "% for a term of "+term[ratePlace]+" years is \n" +"$"+ (df.format(totalInt) +" Dollars\n"))); System.out.println("\nThe total amount of loan plus interest is $"+(df.format(amount)+" Dollars.\n")); System.out.println("\nThe Payments spread over "+term[ratePlace]* 12+" months would be $"+ (df.format (moPay) + " Dollars a month\n\n")); System.in.skip(System.in.available()); System.out.println("\nWould you like to see a planned payment schedule? y for Yes, or x to Exit"); answer = (char)System.in.read(); if (answer == 'y') { System.out.println((term[ratePlace]*12) + " monthly payments:"); String monthlyPayment = df.format(moPay); for (int a = 1; a <= firstIterate; a++) { System.out.println(" Payment Schedule \n\n "); System.out.println("Month Payment Balance\n"); for (int b = 1; b <= secondIterate; b++) { amount -= Double.parseDouble(monthlyPayment); System.out.println(""+ pmt++ +"\t"+ monthlyPayment + "\t"+df.format(amount)); } System.in.skip(System.in.available()); System.out.println(("This Page Is Complete Press [ENTER] to Continue")); System.in.read(); } } } } }