Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why does this give me an error?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xtreemboarder




PostPosted: Wed Mar 21, 2007 9:08 pm   Post subject: Why does this give me an error?

Quote:
private static int countMoves (char[] [] maze)
{
int moveCount = 0;
for (int i = 0 ; i < maze.length ; i++)
{
for (int j = 0 ; i < maze [1].length ; j++)
{
if (maze [i] [j] == '*')
moveCount++;
}
}
return moveCount;
}

It throws an array index out of bounds exception... why?[/quote]
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Wed Mar 21, 2007 9:13 pm   Post subject: RE:Why does this give me an error?

Let's look at how you declared the for loops.
code:
for (int i = 0 ; i < maze.length ; i++)

code:
for (int j = 0 ; i < maze [1].length ; j++)
Hm... the second for loop seems a wee bit off. Change the i to j, and what's up with the one in "[1].length"?
Clayton




PostPosted: Wed Mar 21, 2007 9:18 pm   Post subject: Re: Why does this give me an error?

because he seems to have a 2 - dimensional array, and he wants to find the upper bounds of the second dimension of the array, he simply has to find the upper bound of the array from an element of the first array. Recall that 2D arrays are really just an array of an arrays Razz
klopyrev




PostPosted: Wed Mar 21, 2007 10:13 pm   Post subject: Re: Why does this give me an error?

Aside from the fact that the line in the second for loop should be j< not i<, using maze[1].length can create an exception when the array is size 1. Suppose the array was declared maze = new char[1][1]; Calling maze[1] will result in an exception. Use maze[0].length!!! If the arrays can have different sizes, then use maze[i].length!!!

KL
xtreemboarder




PostPosted: Thu Mar 22, 2007 8:54 am   Post subject: Re: RE:Why does this give me an error?

rdrake @ Wed Mar 21, 2007 9:13 pm wrote:
Let's look at how you declared the for loops.
code:
for (int i = 0 ; i < maze.length ; i++)

code:
for (int j = 0 ; i < maze [1].length ; j++)
Hm... the second for loop seems a wee bit off. Change the i to j, and what's up with the one in "[1].length"?


thanks for your help... i didn't even notice that!
ICS_hard




PostPosted: Sat Jan 05, 2008 6:50 pm   Post subject: RE:Why does this give me an error?

private void initConfig() {
/* TO DO: initialize the following variables with information read from the config file
* - MAXGAME
* - logoIcon
* - iconFile
*/
String snum;
int MAXGAME;
String [] iconFile=new String[NUMPLAYER];
String CONFIGFILE =("config.txt");
try {

BufferedReader in = new BufferedReader (new FileReader (CONFIGFILE));
snum= in.readLine();
while (snum != null){
MAXGAME = Integer.parseInt (snum);
snum= in.readLine ();
logoIcon=snum;

for (int i = 0; i < NUMPLAYER; i++){
snum=in.readLine ();
iconFile[i]=snum;
}
}
in.close();
}catch (IOException iox){
System.out.print ("Incorrect datatype: ");
}
}

// in config file
3
logo.bmp
spongebob.bmp
patrick.bmp
ICS_hard




PostPosted: Sat Jan 05, 2008 6:53 pm   Post subject: RE:Why does this give me an error?

why does the code above give me an exception.
NumberFormatException: For input string: "patrick.bmp"
i have used alll kinds of methods to resolve it. i even tried -trial and error- nothig seems to work. can someone help me? please
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: