# Help with arrays!!

• 05-24-2011, 01:40 AM
bamagirl31
Help with arrays!!
I have a question. Say I have an application and variables established. But the variables which have been established need to be put into arrays. How do I do this? One of the variables is used to calculate another variable. How do I form an array for these two? Thanks.
• 05-24-2011, 02:16 AM
Fubarable
Can you give us more detail on just what it is you are trying to do? Is this an academic exercise? If so, have you been given certain instructions and constraints, and if so, can you post them here?
• 05-24-2011, 02:50 AM
bamagirl31
Explanation
Hi. I have an application set up with two variables such as x = term * 12; and y = rate /12; How can I set up term and rate with different amounts using an array if they are already initialized at 0? Can I set that up a different way? Thanks. I hope that better explains what I am attempting to do.
• 05-24-2011, 03:13 AM
bamagirl31
Hi, basically the loan term and interest rate will vary but they are used to obtain other values. I do not know where I am going wrong.

package mortgages;

/**The Mortgage class implements an application which calculates the mortgage
*payment, interest paid, principal paid and loan balance.
* The application prints interest paid, mortgage payment, and loan balance
* to output
* @author Lekeisha
**/
import java.io.IOException;
public class Mortgages
/**Creates a new instance of Mortgage**/
{

public static void main(String[] args)
{
double[]loanTerm = {7, 15, 30};
double[]intRates = {.0535, .0550, .0575};//initiates the monthly interest rate
calmonintrate(intRates);
caltotmons(loanTerm);
}
public static void calmonintrate(double intRates[])
{
for(int counter = 0; counter<intRates.length;counter++)
intRates[counter]/12;
}
public static void caltotmons(double loanTerm[])
{
for(int counter = 0; counter<loanTerm.length;counter++)
loanTerm[counter]*12;
}

monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
//calculates the mortgage payment
intsPaid = amLoanOne*monIntRate;
//calculates the interest paid

princPaid = monPay - intsPaid;
//calculates the principal paid
amLoanNxt = amLoanOne - princPaid;
//calculates the amount of loan balance after initial amount

for(int i = 0; i < 31; i++)
{
intsPaid+=i;//counter for interest paid
princPaid+=i;//counter for principal paid
amLoanNxt+=i;//counter for amount of loan balance after initial amount

System.out.println("Interest amount is \$" + Math.round(intsPaid)+ ".");
//calculates the interest paid with interest rate of 5.75%
System.out.println("Mortgage payment amount is \$" + Math.round(monPay)+ ".");
//calculates the mortgage payment amount
System.out.println("Loan balance amount is \$" + Math.round(amLoanNxt)+ ".");
//calculates the loan balance
System.out.println("(press Enter to exit)");
//asks user input of 'Enter' to display other lines of text
try
{
}
catch (IOException e)
{
return;

}
}
}
}
• 05-24-2011, 03:52 AM
Norm
PLEASE add code tags to your code to preserver formatting. It is hard to see where matching { and } are. Some of your code appears to be outside of any method.

Quote:

I do not know where I am going wrong.
Can you explain? If the output is incorrect, please show what the current output is and explain the problem with it and show what it should be.

Quote:

How can I set up term and rate with different amounts using an array if they are already initialized at 0?
can you give an example of what you want to do? This makes no sense to me.

Can you comment your code describing what each statement is supposed to do?
For example, what is the following to do:

intRates[counter]/12;
• 05-24-2011, 03:56 AM
Fubarable
Quote:

Originally Posted by bamagirl31
Hi. I have an application set up with two variables such as x = term * 12; and y = rate /12; How can I set up term and rate with different amounts using an array if they are already initialized at 0? Can I set that up a different way? Thanks. I hope that better explains what I am attempting to do.

This information is helpful because I find that often the best answer is not the one the original poster asks for, and that's the case here. I think of an array as a container to hold very similar entities such as a bunch of grades, or the heights of students in a class. Your problem on the other hand requires that you store two entities that have different meaning, one representing the length of a loan and the other the interest rate, and here you do not want to use an array but rather a new class, one with a term field and a rate field. Then you can create objects of this class and have them each hold a term value and a rate value, and can even create an array of these objects if desired.

As for your code above, you're going to want to edit it and add [code][/code] tags (the first tag above your code block and the second below) so that the pasted code will retain its formatting and be readable.
• 05-24-2011, 04:18 AM
bamagirl31
Posted code with Code tags
Quote:

Originally Posted by Norm
PLEASE add code tags to your code to preserver formatting. It is hard to see where matching { and } are. Some of your code appears to be outside of any method.

Can you explain? If the output is incorrect, please show what the current output is and explain the problem with it and show what it should be.

can you give an example of what you want to do? This makes no sense to me.

Can you comment your code describing what each statement is supposed to do?

For example, what is the following to do:

intRates[counter]/12;

Code:

```package surveys; /**  *  * @author Lekeisha  */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Surveys {     public Surveys()             {             }      public static void main(String[] args)         {                Surveys surveys1 = new Surveys();         surveys1.inputFromConsole();         }         public void inputFromConsole()             {                 int answer1;                 int answer2;                 int tryAgain;                             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                                             System.out.println("How many kids do you have?" );                                                 try             {                    answer1 = br.read();             }             catch             (IOException e)             {                 return;             }                    System.out.println("How many pets do you have?");                         try             {                    answer2 = br.read();             }             catch             (IOException e)             {                      return;             }                            System.out.println("Do you want to enter another set of answers? Enter 01 for Yes or 02 for No");                             try             {                    tryAgain = br.read();             }             catch             (IOException e)                    {                  return;             }                                            }         // TODO code application logic here                           }                       // TODO code application logic here```
• 05-24-2011, 04:25 AM
Norm
What is the purpose of your last post? You should start a new thread with a new problem.