Welcome to Dream.In.Code
Become a Java Expert!

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!




Needs an Array , and calculation correction

2 Pages V  1 2 >  
Reply to this topicStart new topic

Needs an Array , and calculation correction, Java Assistance needed

mysong
31 May, 2008 - 01:28 PM
Post #1

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

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();
      }
    }
  }
}



User is offlineProfile CardPM
+Quote Post

mysong
RE: Needs An Array , And Calculation Correction
2 Jun, 2008 - 08:10 PM
Post #2

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

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();
      }
    }
  }
}



User is offlineProfile CardPM
+Quote Post

mensahero
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 02:40 AM
Post #3

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions


hello .. First read this tutorial to better understand arrays..

Array Tutorial


biggrin.gif biggrin.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

mysong
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 07:11 PM
Post #4

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

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
User is offlineProfile CardPM
+Quote Post

pbl
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 07:41 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
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");


or something like that:

CODE

        int[] firstIterateValues = {4, 8, 15};
        int[] secondIterateValues = {21, 15, 24};
        int[] totalMoValues = {84, 180, 360};    


and

CODE

    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");

        answer = (char)System.in.read();

        if (answer == '4') //exit or quit
        {
            System.exit(0);
        }
        else if(answer < '1' || answer > '3') {
            System.in.skip(System.in.available());               //validates choice
            System.out.println("Invalid choice, try again.\n\n");
            validChoice = false;
            continue;
        }
     // OK valid answer
        ratePlace = answer - '1';
        firstIterate = firstIterateValues[ratePlace];
        secondIterate = secondIterateValues[ratePlace];
        totalMo = totalMoValues[ratePlace];


This post has been edited by pbl: 3 Jun, 2008 - 07:53 PM
User is online!Profile CardPM
+Quote Post

mysong
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:15 PM
Post #6

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

the values that the users will see on the screen...is what he wants in an array, as the outcome
User is offlineProfile CardPM
+Quote Post

mysong
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:27 PM
Post #7

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

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

smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:29 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mysong @ 3 Jun, 2008 - 09:15 PM) *

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]...
}



User is online!Profile CardPM
+Quote Post

mysong
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:35 PM
Post #9

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

what area of mine would I replace with
I see the area but ..I am a bit confused.
I do know that [] is the array

CODE

int[] firstIterateValues = {4, 8, 15};
        int[] secondIterateValues = {21, 15, 24};
        int[] totalMoValues = {84, 180, 360};    


the code I posted was my last grade.
here is his comment to correct my week 5

"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."


User is offlineProfile CardPM
+Quote Post

pbl
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:39 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mysong @ 3 Jun, 2008 - 09:30 PM) *

what area of mine would I replace with
I see the area but ..I am a bit confused.
I do know that [] is the array

CODE

int[] firstIterateValues = {4, 8, 15};
int[] secondIterateValues = {21, 15, 24};
int[] totalMoValues = {84, 180, 360};    



firstIterateValues is an array
firstIterateValues[0] = 4;
firstIterateValues[1] = 8;
firstIterateValues[2] = 15;

if you had only firstIterate to set then
CODE

if(answer == '1')
   firstIterate = 4;
else if(answer == '2')
  firstIterate = 8;
else...

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

firstIterate = firstIterateValues[ratePlace];
secondIterate = secondIterateValues[ratePlace];
totalMo = totalMoValues[ratePlace];

I had to change the order into which you checked for '0' and '4' but that's it



User is online!Profile CardPM
+Quote Post

pbl
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:45 PM
Post #11

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mysong @ 3 Jun, 2008 - 09:35 PM) *

"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."


So click on "This post was usefull" tab smile.gif

User is online!Profile CardPM
+Quote Post

mysong
RE: Needs An Array , And Calculation Correction
3 Jun, 2008 - 08:47 PM
Post #12

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 33

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");

        answer = (char)System.in.read();

        if (answer == '4') //exit or quit
        {
            System.exit(0);
        }
        else if(answer < '1' || answer > '3') {
            System.in.skip(System.in.available());               //validates choice
            System.out.println("Invalid choice, try again.\n\n");
            validChoice = false;
            continue;
        }
     // OK valid answer
        ratePlace = answer - '1';
        firstIterate = firstIterateValues[ratePlace];
        secondIterate = secondIterateValues[ratePlace];
        totalMo = totalMoValues[ratePlace];

     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();
      }
    }
  }
}
}

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter