you will now add a breakpoint to the line for (int j=1; j<=6; j++). You do this
ID: 3915799 • Letter: Y
Question
you will now add a breakpoint to the line for (int j=1; j<=6; j++). You do this by right-clicking in this line at the very left.Run your program in the debug mode by clicking on Run - Debug(alternatively, F11).
Now you can use F5 to execute your program one instruction at a time (called single-stepping) or F8 to continue with the execution until the next breakpoint is reached (in a loop the next breakpoint might be the same one again).
Fix the error when you find it. DebuggingExercise.java x import java.io.*; public class DebuggingExercisef public static void main (String[] args) int[] [] testArray new int [5] [6] ; for(int i-0 ; iSi++) for(int j-1 j-6; j++ testArray[il[j]
Explanation / Answer
Initially i = 0,
testArray[0][1] = ( 0 + 1 ) * 1
= 1
testArray[0][2] = ( 0 + 1 ) * 2
= 2
testArray[0][1] = ( 0 + 1 ) * 3
= 3
testArray[0][1] = ( 0 + 1 ) * 4
= 4
testArray[0][1] = ( 0 + 1 ) * 5
= 5
It causes a IndexOutOfBounds Exception as the size of testArray is 5x6. So, each column goes from 0 to 5. But we try to access with column 6, which causes exception.
Correct Code
import java.io.*;
public class DebuggingExercise{
public static void main(String[] args)
{
int[][] testArray = new int[5][6];
for( int i = 0 ; i < 5 ; i++ )
{
// j goes from 0 to 5
for( int j = 0 ; j < 6 ; j++ )
testArray[i][j] = ( i + 1 ) * j;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.