Computer Science Canada Why does this give me an error? |
Author: | xtreemboarder [ 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] |
Author: | rdrake [ 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.
|
Author: | Clayton [ 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 ![]() |
Author: | klopyrev [ 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 |
Author: | xtreemboarder [ 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.
thanks for your help... i didn't even notice that! |
Author: | ICS_hard [ 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 |
Author: | ICS_hard [ 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 |