13 Replies - 5507 Views - Last Post: 06 July 2011 - 06:25 PM Rate Topic: -----

#1 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Not sure how to access all array values I created

Posted 05 July 2011 - 10:10 PM

The code runs and calculates as I need it
but I need to change the array value after the balance reaches 0 and set the balance back to 200,000- twice.

import java.text.*;     
public class my3prg420
                           {
                      public static void main(String[] args)
{    

            
	int yearsArray[] = { 7, 15, 30 };		 
	double InterestArray[] = { 5.35, 5.5, 5.75 };	
         
   double balance = 200000.00;   
   double newbalance = 200000.00;
   double monthly_payment;
   double monthly_principal;
   double monthly_interest;
  NumberFormat nf = NumberFormat.getCurrencyInstance();
   double y_rate = (InterestArray[2]/100/12);
	  monthly_payment = balance * (y_rate/ (1-Math.pow((1+y_rate),-1* (yearsArray[2] *12))));
 
  System.out.println("\t\t\t\tAmortization Schedule -\tBorrowed amount is $200,000.\n"+
                     "\t\t\t\t\tThis loan is for 30 years @ 5.75% interest annually.\n"+
							"\t\t\t\t\t\tThe monthly payment is-" +
							 nf.format(monthly_payment)+"\n");
			


  for (int i = 1; i <= yearsArray[2]*12; i++) { 
     monthly_interest = (balance*y_rate);
     monthly_principal = (monthly_payment - monthly_interest);
     balance = (balance - monthly_principal);

  System.out.println("\tBalance remaining " + nf.format(balance)+
                     "\t\tInterest paid this payment " + nf.format(monthly_interest)+ 
                     "\t\tPrincipal paid this payment" + nf.format(monthly_principal)+"\n");
						
  try {Thread.sleep(5);} catch (InterruptedException a){}
  if(balance < 1);
          Stuck here

}   System.out.println("\t\t\t\tAmortization Schedule -\tBorrowed amount is $200,000.\n"+
                     "\t\t\t\t\tThis loan is for 30 years @ 5.75% interest annually.\n"+
							"\t\t\t\t\t\tThe monthly payment is-" +
							 nf.format(monthly_payment)+"\n");   
						 
						
}
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Not sure how to access all array values I created

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 10:19 PM

Why not make a method that takes the array values and the balance, inside it put your code for computation. When the computation ends, return the value you need to change for the next run and pass it now to the same function again, since you mentioned you only need one value to change and the other is a constant variable(the 200,000). Make a do while covering that function invoked that checks if how many passes it has already done so that you can set it to run two times.

By the way your topic title is kinda misleading. If you're asking how to change the value of an array simply do
nameOfArray[index]=yourNewValue;

but if it isn't your question then I trust the opinion on the start of my post will help you out.

This post has been edited by fromTheSprawl: 05 July 2011 - 10:23 PM

Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,259
  • Joined: 27-December 08

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 10:20 PM

Sounds like nested loops, with the outer loop being a for loop.
Was This Post Helpful? 2
  • +
  • -

#4 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 10:28 PM

Thank you for the prompt response.
So I should use a return statement after the balance reaches 0 that sets the new values. I tried this earlier and kept getting an error trying to rest the balance.

View PostfromTheSprawl, on 05 July 2011 - 11:19 PM, said:

Why not make a method that takes the array values and the balance, inside it put your code for computation. When the computation ends, return the value you need to change for the next run and pass it now to the same function again, since you mentioned you only need one value to change and the other is a constant variable(the 200,000). Make a do while covering that function invoked that checks if how many passes it has already done so that you can set it to run two times.

Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 10:48 PM

May I ask what is the value on the array you need to change on your next passes?

This post has been edited by fromTheSprawl: 05 July 2011 - 10:49 PM

Was This Post Helpful? 1
  • +
  • -

#6 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 11:06 PM

The values are - 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%
The program calculates 30 years at 5.75 first, then either of the others next. So the next value could be either of the remaining two values as long as it corresponds with its partner either year or interest. Thanks again.

View PostfromTheSprawl, on 05 July 2011 - 11:48 PM, said:

May I ask what is the value on the array you need to change on your next passes?

Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 11:29 PM

macosxnerd01 has presented you with a simpler solution then, nested for loops!To do it what you need to do is:

public static void main(String[] args)
{
//first loop will cycle to your interest values
for(int i = 0; i<=theArrayYouNeedToCycleValuesWith.length; i++)
{

/*your original for loop inside along with your computations.
The only change you need to do now is instead of specifying a specific index number of array for your interest array, use the variable on the outermost for loop to specify what interest value it's now holding. In this case it's i.
*/
}
}



By the way, you don't need to do a method anymore or care what to do when your first run through reaches zero. It will just return to the outermost for loop, increment its value, and reset all values in your inner for loop. The only change is that now you are working with a next interest value.

Also, what I'm telling you now will only work for the age 30. I'm not sure how you want to work inside your for loop. Do you want to just simply run the other ages with their corresponding interests(same index numbers)? Or could other ages also use the other interest rates?

In what I've presented you it will print all the possible values for age 30 using all the possible interests. If you want to do it for each age with it's corresponding interest then you should simply ditch the specific index for your age array and change it as the same with the variable you used to access on your interest array.

This post has been edited by fromTheSprawl: 05 July 2011 - 11:41 PM

Was This Post Helpful? 1
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 11:39 PM

Double post, sorry!

This post has been edited by fromTheSprawl: 05 July 2011 - 11:40 PM

Was This Post Helpful? 1
  • +
  • -

#9 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 11:46 PM

Yes the years array index should work with and coincide with the interest array so that i get three sets of output, one for each loan duration with corresponding interest.

View PostfromTheSprawl, on 06 July 2011 - 12:29 AM, said:

macosxnerd01 has presented you with a simpler solution then, nested for loops!To do it what you need to do is:

public static void main(String[] args)
{
//first loop will cycle to your interest values
for(int i = 0; i<=theArrayYouNeedToCycleValuesWith.length; i++)
{

/*your original for loop inside along with your computations.
The only change you need to do now is instead of specifying a specific index number of array for your interest array, use the variable on the outermost for loop to specify what interest value it's now holding. In this case it's i.
*/
}
}



By the way, you don't need to do a method anymore or care what to do when your first run through reaches zero. It will just return to the outermost for loop, increment its value, and reset all values in your inner for loop. The only change is that now you are working with a next interest value.

Also, what I'm telling you now will only work for the age 30. I'm not sure how you want to work inside your for loop. Do you want to just simply run the other ages with their corresponding interests(same index numbers)? Or could other ages also use the other interest rates?

In what I've presented you it will print all the possible values for age 30 using all the possible interests. If you want to do it for each age with it's corresponding interest then you should simply ditch the specific index for your age array and change it as the same with the variable you used to access on your interest array.

Was This Post Helpful? 0
  • +
  • -

#10 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,063
  • Joined: 28-December 10

Re: Not sure how to access all array values I created

Posted 05 July 2011 - 11:57 PM

Then all the posts above will help you, I've just tried it and it works.
DreamInCode firmly believes that you should do the coding while we help you solve the problems.
Here is a faint outline of what you should code:

//outer for loop for interest array
for(int i = 0;i < lengthOfInterestArray; i++)
{
//inner for loop which is your old for loop in your computation
}



The parts where you placed a specific index for example:
InterestArray[2]


Should now be changed with:
InterestArray[forVariable]



Then place your printlns in the proper place, change it a bit to look like this:
System.out.println("This loan is for " currentAgeArrayValue "+" @ "+ currentInterestArrayValue +" % interest annually.");


That will accomodate all the prints for all array values without extraneous printlns.

We believe you can do it, good luck! ^^
Was This Post Helpful? 1
  • +
  • -

#11 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Not sure how to access all array values I created

Posted 06 July 2011 - 12:09 AM

All of your posts are very helpful and right to the exact points.I will return and thanks again.

View PostfromTheSprawl, on 06 July 2011 - 12:57 AM, said:

Then all the posts above will help you, I've just tried it and it works.
DreamInCode firmly believes that you should do the coding while we help you solve the problems.
Here is a faint outline of what you should code:

//outer for loop for interest array
for(int i = 0;i < lengthOfInterestArray; i++)
{
//inner for loop which is your old for loop in your computation
}



The parts where you placed a specific index for example:
InterestArray[2]


Should now be changed with:
InterestArray[forVariable]



Then place your printlns in the proper place, change it a bit to look like this:
System.out.println("This loan is for " currentAgeArrayValue "+" @ "+ currentInterestArrayValue +" % interest annually.");


That will accomodate all the prints for all array values without extraneous printlns.

We believe you can do it, good luck! ^^

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Not sure how to access all array values I created

Posted 06 July 2011 - 02:21 PM

You know computer memory is not like a blackboard, you do not have to erase it before using it.
If you have loops that compute certain elements and store the values in an array, no need to re-initialize that array at the next iteration of the loop, at least not the elements that are not used as input values.

Remembers me, 10 years ago, a customer who had performance problem on a Cobol application.
It was the billing system of a cellular phone company. They had, for each customer an array of 25,000 entries for the SMS send/received by a customer during the month. Before processing a new customer the first instruction was

INITIALIZE SMS_ARRAY.

For the ones who are not familliar with COBOL: INITIALIZE is a clever instruction which initialize a structure (or an array of structures) moving spaces to alphabetic fields, 0 to computable fields and '0' to numeric fields.
Each of the array element contained about 20 fields. The 25,000 entries were initialized every time the program changed customer even if that partcular customer didn't send/received any SMS that month.
Stop ! Halt ?
Have an entry NB_SMS_USED, set it to 0 and then if an SMS has to be entered, initialize SMS_ARRAY[NB_SMS_USED], only the rows you actually need. That is where I had to explain to the customer's programmer: "You know computer memory is not like a blackboard, you do not have to erase it before using it."

:^:

This post has been edited by pbl: 06 July 2011 - 05:47 PM

Was This Post Helpful? 2
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Not sure how to access all array values I created

Posted 06 July 2011 - 05:48 PM

Oups... by bad. Sorry
Was This Post Helpful? 0
  • +
  • -

#14 wizardcloak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 22-December 09

Re: Not sure how to access all array values I created

Posted 06 July 2011 - 06:25 PM

//Thank you for the tips everyone. Here is a portion of the revised code that works well.
I still need to add one more section that calculates the last array values [0].

import java.text.*;     
public class my3prg420
                           {
                      public static void main(String[] args)
{    

            
	int yearsArray[] = { 7, 15, 30 };		 
	double InterestArray[] = { 5.35, 5.5, 5.75 };	
         
   double balance = 200000.00;   
   double newbalance = 200000.00;
   double monthly_payment;
   double monthly_principal;
   double monthly_interest;
  NumberFormat nf = NumberFormat.getCurrencyInstance();
 if (balance < 0.01) { newbalance = (200000); double newy_rate = (InterestArray[1]/100/12);
		 System.out.println("This loan is for " + yearsArray[1]  + 
                 " years @ " + InterestArray[1] + " % interest annually.");	

	  monthly_payment = newbalance * (newy_rate/ (1-Math.pow((1+newy_rate),-1* (yearsArray[1] *12))));
	  System.out.println("The monthly payment is-" + nf.format(monthly_payment)+"\n");
	  for (int j = 1; j <= yearsArray[1]*12; j++) { 
     monthly_interest = (newbalance*newy_rate);
     monthly_principal = (monthly_payment - monthly_interest);
     newbalance = (newbalance - monthly_principal);
	  System.out.println("\tBalance remaining " + nf.format(newbalance)+
                     "\t\tInterest paid this payment " + nf.format(monthly_interest)+ 
                     "\t\tPrincipal paid this payment" + nf.format(monthly_principal)+"\n");
 }


View Postpbl, on 06 July 2011 - 03:21 PM, said:

You know computer memory is not like a blackboard, you do not have to erase it before using it.
If you have loops that compute certain elements and store the values in an array, no need to re-initialize that array at the next iteration of the loop, at least not the elements that are not used as input values.

Remembers me, 10 years ago, a customer who had performance problem on a Cobol application.
It was the billing system of a cellular phone company. They had, for each customer an array of 25,000 entries for the SMS send/received by a customer during the month. Before processing a new customer the first instruction was

INITIALIZE SMS_ARRAY.

For the ones who are not familliar with COBOL: INITIALIZE is a clever instruction which initialize a structure (or an array of structures) moving spaces to alphabetic fields, 0 to computable fields and '0' to numeric fields.
Each of the array element contained about 20 fields. The 25,000 entries were initialized every time the program changed customer even if that partcular customer didn't send/received any SMS that month.
Stop ! Halt ?
Have an entry NB_SMS_USED, set it to 0 and then if an SMS has to be entered, initialize SMS_ARRAY[NB_SMS_USED], only the rows you actually need. That is where I had to explain to the customer's programmer: "You know computer memory is not like a blackboard, you do not have to erase it before using it."

:^:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1