Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-06-2012, 07:27 PM PM User | #1 KenanCross New to the CF scene   Join Date: Feb 2012 Posts: 3 Thanks: 2 Thanked 0 Times in 0 Posts New to CF and new to Java! Please help! Hi all, I'm enrolled in a Java class and I'm having trouble with an array in a for loop. Here's the entire code: Code: ```import java.text.DecimalFormat; /** * * @author KenanCross */ public class MortgageCalcArray { int i; // counter int termLimit; int payments; double balPdCalc; //Calculated result of balPd and balPd2 double loanCalc; //Power of Calculation for compound interest double[] rates = {0.0525,0.055,0.0575}; //Array containing interest rates double[] results = new double[3]; //Array containing computed results int[] lengths = {7,15,30}; //Array containing values of corresponding years. double annualRate; //Computed Annual Rate double interestPlus; // rate + 1 double interestAdj; double interestExp; double principle; /* Principle loan amount */ double monthlyPayment; /* Calculated Monthly Payment */ DecimalFormat dec; DecimalFormat percentage; public static void main (String args[]){ MortgageCalcArray calc = new MortgageCalcArray(); calc.calculatePaymentAmount(); calc.printPaymentAmount(); } public MortgageCalcArray(){ principle = 200000; dec = new DecimalFormat ("\$0.00"); percentage = new DecimalFormat("0.00%"); } public void calculatePaymentAmount(){ for (i=0; i <= lengths.length; i++) annualRate = (rates[i]/(12)); payments = (12) * (lengths[i]); interestAdj = (annualRate)+(1); interestExp = Math.pow(interestAdj, payments); results[i] = principle * (annualRate * interestExp)/(interestExp - 1); } public void printPaymentAmount (){ System.out.println("Your Loan Amount is "+ dec.format(principle)); System.out.println("\t Your monthly payment at " + (lengths[0]) + " years, with a " + percentage.format (rates[0]) + " rate, is " + dec.format (results[0])); } }``` I'm trying to get the for loop to call the different values in the array in conjunction with each loop iteration from variable i, then store the results in the results array at the bottom. I keep getting a java.lang.ArrayIndexOutOfBoundsException: 3 throw error, yet I can't figure out where I went wrong. Any help you can give would be greatly appreciated!
 02-07-2012, 03:15 AM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,603 Thanks: 4 Thanked 2,598 Times in 2,567 Posts Java is 0 based. When you iterate up to a size using array.length, that would say you have 3 items within your array. These items are located at 0, 1, and 2. When you use <= it is moving from 0 through 3 inclusive, so its attempting to access a 4th element and tosses that error.
 Users who have thanked Fou-Lu for this post: KenanCross (02-12-2012)
 02-12-2012, 10:31 PM PM User | #3 KenanCross New to the CF scene   Join Date: Feb 2012 Posts: 3 Thanks: 2 Thanked 0 Times in 0 Posts Thanks for your response! I was able to figure it out.

 Bookmarks

 Tags arrays, java

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:54 AM.