Reputation: 159
I have written a program in which the user inputs the dimensions of a two-dimensional array, then the computer prints out the table with those dimensions and fills it with random integers from 0 to 9. Then, if there are four consecutive equal integers that appear anywhere in the table, the computer would return "True". If there are no consecutive equal integers in the table, it would return "False". For instance:
2 5 8 7 1
3 2 9 4 7
5 1 2 0 3
8 0 1 2 7
In that table, two appear consecutively, diagonally from the first spot. It can also be like this:
9 5 3 7 0
2 5 7 3 1
8 5 0 2 9
4 5 1 7 5
In this table, five appear vertically down from the second spot.
Here is the code I have written:
/*MyName*/
package fourconsecutivenumbers;
import java.util.Random;
import java.util.Scanner;
public class FourConsecutiveNumbers {
public static void main(String[] args) {
Scanner rowDimension = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int firstInput = rowDimension.nextInt();
Scanner columnDimension = new Scanner(System.in);
System.out.print("Enter the number of columns: ");
int secondInput = columnDimension.nextInt();
int[][] randomTable = new int[firstInput][secondInput];
for (int row = 0; row < firstInput; row++) {
for (int column = 0; column < secondInput; column++) {
randomTable[row][column] = (int)(Math.random() * 10 + 0);
System.out.print(randomTable[row][column] + " ");
}
System.out.println();
}
if(check_horizontal(randomTable) || check_vertical(randomTable) || check_diagonal(randomTable)){
System.out.println("True");
}
else {
System.out.println("False");
}
}
public static boolean check_vertical(int[][] randomTable) {
for (int i = 0; i<randomTable.length-3; i++){
for(int t=0; t<randomTable[0].length-3;t++){
if (randomTable[i][t]==randomTable[i][t+1] && randomTable[i][t+1]==randomTable[i][t+2] && randomTable[i][t+2]==randomTable[i][t+3]){
return true;
}
}
}
return false;
}
public static boolean check_horizontal(int[][] randomTable){
for (int i = 0; i<randomTable.length; i++){
for(int t=0; t<randomTable[0].length;t++){
if (randomTable[t][i]==randomTable[t+1][i] && randomTable[t+1][i]==randomTable[t+2][i] && randomTable[t+2][i]==randomTable[t+3][i]){
return true;
}
}
}
return false;
}
public static boolean check_diagonal(int[][] randomTable){
//check down
for (int t =0; t<randomTable.length-3; t++){
for(int i=0; i<randomTable[0].length-3;i++){
if (randomTable[i][t]==randomTable[i+1][t+1] && randomTable[i+1][t+1]==randomTable[i+2][t+2] && randomTable[i+2][t+2]==randomTable[i+3][t+3]){
return true;
}
}
}
//check up
for (int t = 3; t < randomTable.length; t++){
for(int i=0; i<randomTable[0].length-3;i++){
if (randomTable[t][i]==randomTable[t-1][i+1] && randomTable[t-1][i+1]==randomTable[t-2][i+2] && randomTable[t-2][i+2]==randomTable[t-3][i+3]){
return true;
}
}
}
return false;
}
}
Now the error comes up whenever I put in different dimensions. Like a 5x10 table or a 10x5 table...but if I were to do a 5x5 table or a 10x10 table, it executes. The error I get is...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at fourconsecutivenumbers.FourConsecutiveNumbers.check_horizontal(FourConsecutiveNumbers.java:49)
at fourconsecutivenumbers.FourConsecutiveNumbers.main(FourConsecutiveNumbers.java:25)
Java Result: 1
I am new to programming but I know the error is from line 49. Maybe I wrote the for loop wrong? I don't know what I did wrong really and I was hoping someone can show me what I did wrong and how to fix it so I can learn what not to do.
Upvotes: 0
Views: 93
Reputation: 12028
In the check_horizontal method you have your i's and t's back to front
if (randomTable[t][i]==randomTable[t+1][i] &&
randomTable[t+1][i]==randomTable[t+2][i] &&
randomTable[t+2][i]==randomTable[t+3][i]){
return true;
}
Should be
if (randomTable[i][t]==randomTable[i+1][t] &&
randomTable[i+1][t]==randomTable[i+2][t] &&
randomTable[i+2][t]==randomTable[i+3][t]){
return true;
}
And as pointed out in the comments your loop conditions are also incorrect:
for (int i = 0; i<randomTable.length; i++){
for(int t=0; t<randomTable[0].length;t++){
Should be
for (int i = 0; i<randomTable.length - 3; i++){
for(int t=0; t<randomTable[i].length;t++){
This is checking vertically not horizontally as the name suggests, but as check_vertical is actually checking horizontal this will not adversely effect your program (just your marks).
The first point (i's and t's mixed up) is due to which lengths you are checking i is limited to the length of randomTable (that is the number of rows) while t is limited to the length of the zeroth array t < randomTable[0].length
(that is the number of columns) yet in your code you are using i as an index into columns and t as an index into row.
This may be easier to understand if you renamed i into row, and t into column something like:
private int getNumberOfRows(int[][] table) { return table.length; }
private int getNumberOfColumnsInRow(int[][] table, int row) { return table[row].length }
...
for (int row = 0; row < getNumberOfRows(randomTable) - 3; row++){
for(int column = 0; column<getNumberOfColumnsInRow(randomTable,i);column++){
For the second point loop conditions incorrect as you are adding up to 3 to the row index (the first number in square brackets) you need to ensure there are enough at least 3 more rows after row
.
Upvotes: 2
Reputation: 1838
In the check_horizontal you have swapped i and t. From your loops
for (int i = 0; i<randomTable.length; i++){
for(int t=0; t<randomTable[0].length;t++){
you would need to access the array with
randomTable[i][t]
and not with randomTable[t][i]
Upvotes: 0