Array index out of bounds
Author |
Message |
umaster
![](http://compsci.ca/v3/uploads/user_avatars/180716419545ee0fc722378.jpg)
|
Posted: Mon Mar 24, 2008 12:59 pm Post subject: Array index out of bounds |
|
|
Hey im pretty new to Java just started about 4 weeks ago and so far its ok some things are a bit confusing. Anywho i get this array index out of bounds exception = 5 error. I know that it is checking for a part of the array that doesn't exist but that is not possible seeing as how the loop on goes 5 times and there is definetly 5 values. I clearly am not seeing somthing and i need mores eyes to look at it from a different perspective maybe the problem can be found
Here is my code:
code: |
/*
* Main.java
*
* Created on March 5, 2008, 2:17 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author XXXXX
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double dblAveragenames[]= {0,0,0,0,0};
double dblAverageclass[]= {0,0,0,0,0};
double dblsum = 0;
int intcount3 = 0;
int intCount = 0;
int intcount2 = 0;
String strNames[] = {"Burishnakov","Chow Min","Greg","Bob","Josh"};
String strClass[] = {"Computers","Civics","Chemistry","Bio With Mike O","Physics"};
double dblMarks[][] = {{87.6,83.4,72.5,64.3,67.8},{82.5,68.9,52.0,23.4,83.2},{73.6,77.8,63.1,32.5,72.1},{71.6,73.6,61.0,63.2,75.3},{35.9,58.6,42.3,87.6,13.2}};
for(intCount=0; intCount<=5; intCount++)
{
dblsum = 0;
for (intcount2=0; intcount2<=5; intcount2++)
{
dblsum = dblsum + dblMarks[intCount][intcount2];
if(intCount>=5)
{
dblAveragenames[intCount] = dblsum/5;
}
}
}
for(intcount3=0; intcount3<=5; intcount3++)
{
System.out.println(dblAveragenames[intcount3]);
}
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Mon Mar 24, 2008 1:11 pm Post subject: Re: Array index out of bounds |
|
|
not totally sure here but shouldnt it be :
Java: |
%for(intCount=0; intCount<=5; intCount++) what you put
for(intCount=0; intCount<5; intCount++) % what i think it should be
|
not sure tho... give it a try |
|
|
|
|
![](images/spacer.gif) |
umaster
![](http://compsci.ca/v3/uploads/user_avatars/180716419545ee0fc722378.jpg)
|
Posted: Mon Mar 24, 2008 1:16 pm Post subject: Re: Array index out of bounds |
|
|
ok i tried it and it ran but now at the end i put the third for loop to see if it was doing what i wanted it to do and im gettiong 0.0 when im tryign to display the averages |
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Mon Mar 24, 2008 1:22 pm Post subject: RE:Array index out of bounds |
|
|
code: | double dblAveragenames[]= {0,0,0,0,0}; | this have 5 elements, therefore the "length" of this array is 5, and java is 0-based index and so index is 0 - 4
and now in your for loops you have
code: | for(intCount=0; intCount<=5; intCount++) | if you print out every intCount, the result should be, "0 1 2 3 4 5" notice we have 5, and you are trying to access index of 5, which does not exist according to your array
also another note, for your for loop, you can initialize variables within the first block, for example code: | for (int count = 0; count < 5; count++) |
|
|
|
|
|
![](images/spacer.gif) |
umaster
![](http://compsci.ca/v3/uploads/user_avatars/180716419545ee0fc722378.jpg)
|
Posted: Mon Mar 24, 2008 1:26 pm Post subject: Re: Array index out of bounds |
|
|
ok im not getting the array out of index anymore but now its not getting any values i changed it so that is is now:
code: |
for(intCount=0; intCount<5; intCount++)
|
and it runs but when is prints out the averages at the end apparently they are all 0.0
here are the results:
Quote:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Gary2\My Documents\NetBeansProjects\JavaApplication3\build\classes
compile:
run:
0.0
0.0
0.0
0.0
BUILD SUCCESSFUL (total time: 0 seconds)
|
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Mon Mar 24, 2008 1:36 pm Post subject: Re: Array index out of bounds |
|
|
first of all I fixed your code made it cleaner [well imo]
code: |
public static void main(String[] args) {
// TODO code application logic here
double dblAveragenames[]= {0,0,0,0,0};
double dblAverageclass[]= {0,0,0,0,0};
double dblsum = 0;
String strNames[] = {"Burishnakov","Chow Min","Greg","Bob","Josh"};
String strClass[] = {"Computers","Civics","Chemistry","Bio With Mike O", "Physics"};
double dblMarks[][] = {{87.6,83.4,72.5,64.3,67.8},{82.5,68.9,52.0,23.4,83.2},
{73.6,77.8,63.1,32.5,72.1},{71.6,73.6,61.0,63.2,75.3},{35.9,58.6,42.3,87.6,13.2}};
for(int i=0; i<5; i++)
{
dblsum = 0;
for (int ii=0; ii<5; ii++)
{
dblsum = dblsum + dblMarks[i][ii];
dblAveragenames[i] = dblsum/5;
}
}
for(int iii=0; iii<5; iii++)
System.out.println(dblAveragenames[iii]);
}
} |
your for loops you should do them as this
code: |
for (int varname=0; boolean expression;counter) {bhal...}
what you do is
int varname;
for (varname; boolean expression;counter) {bhal...}
|
you are not going to use the var after the loop then why make it gobal?
EDIT : btw i jsut fixed your code in the reply window do compile it and check it, might miss a semicolon or sometthing silly |
|
|
|
|
![](images/spacer.gif) |
umaster
![](http://compsci.ca/v3/uploads/user_avatars/180716419545ee0fc722378.jpg)
|
Posted: Mon Mar 24, 2008 1:45 pm Post subject: Re: Array index out of bounds |
|
|
ok i got it. It works now im getting real numbers and ive checked them and they work thank you guys
Here is a refrence for those whow might have this problem in the future it is the fixed code:
code: |
package javaproject3;
/*
* Main.java
*
* Created on March 5, 2008, 2:17 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author GAngelucci
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double dblAveragenames[]= {0,0,0,0,0};
double dblAverageclass[]= {0,0,0,0,0};
double dblsum = 0;
String strNames[] = {"Burishnakov","Chow Min","Greg","Bob","Josh"};
String strClass[] = {"Computers","Civics","Chemistry","Bio With Mike O","Physics"};
double dblMarks[][] = {{87.6,83.4,72.5,64.3,67.8},{82.5,68.9,52.0,23.4,83.2},
{73.6,77.8,63.1,32.5,72.1}, {71.6,73.6,61.0,63.2,75.3},{35.9,58.6,42.3,87.6,13.2}};
for(int i=0; i<5; i++)
{
dblsum = 0;
for (int ii=0; ii<5; ii++)
{
dblsum = dblsum + dblMarks[i][ii];
if(ii<=5)
{
dblAveragenames[i] = dblsum/5;
}
}
}
for(int iii=0; iii<5; iii++)
{
System.out.println(dblAveragenames[iii]);
}
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Mon Mar 24, 2008 1:51 pm Post subject: Re: Array index out of bounds |
|
|
why do you need
code: |
if(ii<=5)
{
dblAveragenames[i] = dblsum/5;
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Mon Mar 24, 2008 2:29 pm Post subject: RE:Array index out of bounds |
|
|
code: | double dblAveragenames[]= {0,0,0,0,0};
double dblAverageclass[]= {0,0,0,0,0};
double dblsum = 0; |
Even correct Hungarian notation makes Zombie Jesus cry. This isn't even correct Hungarian notation. |
|
|
|
|
![](images/spacer.gif) |
umaster
![](http://compsci.ca/v3/uploads/user_avatars/180716419545ee0fc722378.jpg)
|
Posted: Wed Apr 02, 2008 12:58 pm Post subject: Re: Array index out of bounds |
|
|
it was an assignment i wonly explained one part dont worry about the rest it works now |
|
|
|
|
![](images/spacer.gif) |
|
|