URGENT!!!! help with array element comparing
ok so i'm writing a program that is to create an 8x8 2d array and fill said array with randomly generated numbers between 0-3, then i have to print said array to the screen. i have these parts dead on, working everytime. now my issue becomes that i have to compare elements in each row, column, diagonal, and sub diagonal to gleen if there are 3 or more consecutive numbers. i then have to print out the row, column, diagonal or sub diagonal containing the consecutive digits and then tell what number is repeated the required min of 3 times. THATS where i am stuck. I have no clue how to do this without just hard coding the whole thing. PLEASE help, this is due very very soon and i am sooooo lost. everything has to be in its own method.
here is what i have so far:
Code java:
import java.util.*; //imports java utilities
public class Project4 { //opens class body
public static void main(String[] args) throws Exception { //opens main method
Scanner keyboard = new Scanner(System.in); //imports scanner object
int array; //declares an integer
int r = 0, c = 0; //declares and initializes two integers
int matrix[][] = new int[8][8]; //declares an array
matrix = getArray(); //initializes array to the getArray method return
printArray(matrix);
rowAnalysis(matrix);
columnAnalysis(matrix);
diagonalAnalysis(matrix);
subAnalysis(matrix);
} //closes main method
/**
* pre-condition: called for from main method.
* post condition: Finishes the array under given parameters
* @return: returns the full array
* @throws Exception
*/
public static int[][] getArray() throws Exception { //opens getArray method body
final int matrix[][] = new int[8][8]; //declares an array
int r = 0, c = 0; //declares and initializes integers
for (r = 0; r < matrix.length; r++) { //opens first for loop
for (c = 0; c < matrix[r].length; c++) { //opens nested for loop
matrix[r][c] = (int) (Math.random() * 4); //gets a random number between 0 and 3
System.out.print(matrix[r][c] + " "); //prints out the number and a space
} //ends nested for loop
System.out.println(); //starts a new line for the 2d array
} //ends first for loop
return matrix;//[r][c]; //returns the printable array to main method once called
} //ends getArray method body
/**
* pre-condition: called for by the main method
* post-condition: Prints the array to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void printArray(int[][] matrix) { //opens printArray method
System.out.print(matrix); //prints the array to the screen
} //ends the printArray method
/**
* pre-condition: called for rowAnalysis by the main method
* post-condition: prints the rowAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void rowAnalysis(int[][] matrix) throws Exception { //opens the rowAnalysis method
for (int r = 0; r < matrix.length - 1; r++){
for (int c = 0; c < matrix.length - 1; c++){
}
}
} //closes the rowAnalysis method
/**
* pre-condition: called for columnAnalysis by the main method
* post-condition: prints the columnAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void columnAnalysis(int[][] matrix) { //opens the columnAnalysis method
} //ends the columnAnalysis method
/**
* pre-condition: called for diagonalAnalysis by the main method
* post-condition: prints the diagonalAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void diagonalAnalysis(int[][] matrix) { //opens the diagonalAnalysis method
} //closes the diagonalAnalysis method
/**
* pre-condition: called for subAnalysis by the main method
* post-condition: prints the subAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void subAnalysis(int[][] matrix) { //opens the subAnalysis method
} //closes the subAnalysis method
} //closes class body
Re: URGENT!!!! help with array element comparing
ok so i have been messing with this stupid thing some more and i have this but its telling me it's getting results in every row for every number ( >.<)
Code java:
public static void rowAnalysis(int[][] matrix) throws Exception { //opens the rowAnalysis method
int zero = 0;
int one = 0;
int two = 0;
int three = 0;
boolean tripZero = false;
boolean tripOne = false;
boolean tripTwo = false;
boolean tripThree = false;
for (int r = 0; r < matrix.length - 1; r++) { //opens for loop
for (int c = 0; c < matrix.length - 1; c++) { //opens nested for loop
if (matrix [r][c] == 0 && matrix [r][c+1] == 0 && matrix[r][c+2] == 0) {
tripZero = true;
c = c + 9;
} //closes if statement
if (matrix [r][c] == 1 && matrix [r][c+1] == 1 && matrix[r][c+2] == 1) {
tripOne= true;
c = c + 9;
} //closes if statement
if (matrix [r][c] == 2 && matrix [r][c+1] == 2 && matrix[r][c+2] == 2) {
tripTwo= true;
c = c + 9;
} //closes if statement
if (matrix [r][c] == 3 && matrix [r][c+1] == 3 && matrix[r][c+2] == 3) {
tripThree= true;
c = c + 9;
} //closes if statement
}//closes nested for loop
if (tripZero = true) { //opens if statement
System.out.println("Got atleast three consecutive 0's in row "
+ (r + 1));
} //closes if statement
if (tripOne = true) { //opens if statement
System.out.println ("Got atleast three consecutive 1's in row "
+ (r + 1));
} //closes if statement
if (tripTwo = true) { //opens if statement
System.out.println("Got atleast three consecutive 2's in row "
+ (r + 1));
} //closes if statement
if (tripThree = true) { //opens if statement
System.out.println("Got atleast three consecutive 3's in row "
+ (r + 1));
} //closes if statement
} //closes for loop
} //closes the rowAnalysis method
Re: URGENT!!!! help with array element comparing
ok update! after staying up most of the night and well into the morning working on this i have figured a lot of it out!!!!
Code java:
import java.util.*; //imports java utilities
public class someName { //opens class body
public static void main(String[] args) throws Exception { //opens main method
Scanner keyboard = new Scanner(System.in); //imports scanner object
int array; //declares an integer
int r = 0, c = 0; //declares and initializes two integers
int matrix[][] = new int[8][8]; //declares an array
matrix = getArray(); //initializes array to the getArray method return
printArray(matrix);
System.out.println("Row Analysis: ");
rowAnalysis(matrix);
System.out.println("Column Analysis: ");
columnAnalysis(matrix);
System.out.println("Diagonal Analysis: ");
diagonalAnalysis(matrix);
System.out.println("Sub-Diagonal Analysis: ");
subAnalysis(matrix);
} //closes main method
/**
* pre-condition: called for from main method.
* post condition: Finishes the array under given parameters
* @return: returns the full array
* @throws Exception
*/
public static int[][] getArray() throws Exception { //opens getArray method body
final int matrix[][] = new int[8][8]; //declares an array
int r = 0, c = 0; //declares and initializes integers
for (r = 0; r < matrix.length; r++) { //opens first for loop
for (c = 0; c < matrix[r].length; c++) { //opens nested for loop
matrix[r][c] = (int) (Math.random() * 4); //gets a random number between 0 and 3
// System.out.print(matrix[r][c] + " "); //prints out the number and a space
} //ends nested for loop
// System.out.println(); //starts a new line for the 2d array
} //ends first for loop
return matrix;//[r][c]; //returns the printable array to main method once called
} //ends getArray method body
/**
* pre-condition: called for by the main method
* post-condition: Prints the array to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void printArray(int[][] matrix) { //opens printArray method
for (int r = 0; r < matrix.length; r++) { //opens first for loop
for (int c = 0; c < matrix[r].length; c++) { //opens nested for loop
System.out.print(matrix[r][c] + " "); //prints out the number and a space
} //ends nested for loop
System.out.println(); //starts a new line for the 2d array
} //ends first for loop
} //ends the printArray method
/**
* pre-condition: called for rowAnalysis by the main method
* post-condition: prints the rowAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void rowAnalysis(int[][] matrix) throws Exception { //opens the rowAnalysis method
int zeroCount = 0;//declares and intializes an integer
int oneCount = 0;//declares and intializes an integer
int twoCount = 0;//declares and intializes an integer
int threeCount = 0;//declares and intializes an integer
for (int r = 0; r < matrix.length; r++) { //opens for loop
for (int c = 0; c < matrix.length; c++) { //opens nested for loop
if (matrix[r][c] == 0) { //opens if statement
zeroCount++;
if (oneCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
oneCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
zeroCount = 0;
} //closes else statement
if (zeroCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 0's in row "
+ (r + 1));
c = c+10;
} //closes if statement
} //closes if statement
if (matrix[r][c] == 1) { //opens if statement
oneCount++;
if (zeroCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
oneCount = 0;
} //closes else statement
if (oneCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 1's in row "
+ (r + 1));
c = c+10;
} //closes if statement
} //closes if statement
if (matrix[r][c] == 2) { //opens if statement
twoCount++;
if (zeroCount < 3 && oneCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
twoCount = 0;
} //closes else statement
if (twoCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 2's in row "
+ (r + 1));
c = c+10;
} //closes if statement
} //closes if statement
if (matrix[r][c] == 3) { //opens if statement
threeCount++;
if (zeroCount < 3 && oneCount < 3 && twoCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
twoCount = 0;
} //closes nested if
else { //opens else statement
threeCount = 0;
} //closes else statement
if (threeCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 3's in row "
+ (r + 1));
c = c+10;
} //closes if statement
} //closes statement
}//closes nested for loop
} //closes for loop
} //closes the rowAnalysis method
/**
* pre-condition: called for columnAnalysis by the main method
* post-condition: prints the columnAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void columnAnalysis(int[][] matrix) { //opens the columnAnalysis method
int zeroCount = 0; //declares and intializes an integer
int oneCount = 0;//declares and intializes an integer
int twoCount = 0;//declares and intializes an integer
int threeCount = 0;//declares and intializes an integer
for (int c = 0; c < matrix.length; c++) { //opens for loop
for (int r = 0; r < matrix.length; r++) { //opens nested for loop
if (matrix[r][c] == 0) { //opens if statement
zeroCount++;
if (oneCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
oneCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
zeroCount = 0;
} //closes else statement
if (zeroCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 0's in column "
+ (c + 1));
} //closes if statement
} //closes if statement
if (matrix[r][c] == 1) { //opens if statement
oneCount++;
if (zeroCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
oneCount = 0;
} //closes else statement
if (oneCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 1's in column "
+ (c + 1));
} //closes if statement
} //closes if statement
if (matrix[r][c] == 2) { //opens if statement
twoCount++;
if (zeroCount < 3 && oneCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
twoCount = 0;
} //closes else statement
if (twoCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 2's in column "
+ (c + 1));
} //closes if statement
} //closes if statement
if (matrix[r][c] == 3) { //opens if statement
threeCount++;
if (zeroCount < 3 && oneCount < 3 && twoCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
twoCount = 0;
} //closes nested if
else { //opens else statement
threeCount = 0;
} //closes else statement
if (threeCount == 3) { //closes if statement
System.out.println("Got atleast three consecutive 3's in column "
+ (c + 1));
} //closes if statement
} //closes statement
}//closes nested for loop
} //closes for loop
} //ends the columnAnalysis method
/**
* pre-condition: called for diagonalAnalysis by the main method
* post-condition: prints the diagonalAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void diagonalAnalysis(int[][] matrix) { //opens the diagonalAnalysis method
int zeroCount = 0;//declares and intializes an integer
int oneCount = 0;//declares and intializes an integer
int twoCount = 0;//declares and intializes an integer
int threeCount = 0;//declares and intializes an integer
for (int r = 0; r < matrix.length; r += 1) { //opens for loop
for (int c = r; c < matrix.length; c = 10) { //opens nested for loop
if (matrix[r][c] == 0) { //opens if statment
zeroCount++;
if (oneCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
oneCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
zeroCount = 0;
} //closes else statement
} //closes if statement
else if (matrix[r][c] == 1) { //opens if statement
oneCount++;
if (zeroCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
oneCount = 0;
} //closes else statement
} //clsoes if statement
else if (matrix[r][c] == 2) { //opens if statement
twoCount++;
if (zeroCount < 3 && oneCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
twoCount = 0;
} //closes else statement
} //closes if statement
else if (matrix[r][c] == 3) { //opens else statement
threeCount++;
if (zeroCount < 3 && oneCount < 3 && twoCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
twoCount = 0;
} //closes nested if
else { //opens else statement
threeCount = 0;
} //closes else statement
} //closes eles statement
} //closes nested for loop
} //closes for loop
if (zeroCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 0's on the "
+ " diagonal.");
} //closes if statement
if (oneCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 1's on the "
+ " diagonal.");
} //closes if statement
if (twoCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 2's on the "
+ " diagonal.");
} //closes if statement
if (threeCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 3's on the "
+ " diagonal.");
} //closes if statement
} //closes the diagonalAnalysis method
/**
* pre-condition: called for subAnalysis by the main method
* post-condition: prints the subAnalysis to the screen
* @param array: sent the integer array from main method
* no return
*/
public static void subAnalysis(int[][] matrix) { //opens the subAnalysis method
int zeroCount = 0;//declares and intializes an integer
int oneCount = 0;//declares and intializes an integer
int twoCount = 0;//declares and intializes an integer
int threeCount = 0;//declares and intializes an integer
for (int c = 0; c < matrix.length; c += 1) { //opens for loop
for (int r = c; r < matrix.length; r = 10) { //opens nested for loop
if (matrix[r][c] == 0) { //opens if statment
zeroCount++;
if (oneCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
oneCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
zeroCount = 0;
} //closes else statement
} //closes if statement
else if (matrix[r][c] == 1) { //opens if statement
oneCount++;
if (zeroCount < 3 && twoCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
twoCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
oneCount = 0;
} //closes else statement
} //clsoes if statement
else if (matrix[r][c] == 2) { //opens if statement
twoCount++;
if (zeroCount < 3 && oneCount < 3 && threeCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
threeCount = 0;
} //closes nested if
else { //opens else statement
twoCount = 0;
} //closes else statement
} //closes if statement
else if (matrix[r][c] == 3) { //opens else statement
threeCount++;
if (zeroCount < 3 && oneCount < 3 && twoCount < 3) { //opens nested if
zeroCount = 0;
oneCount = 0;
twoCount = 0;
} //closes nested if
else { //opens else statement
threeCount = 0;
} //closes else statement
} //closes eles statement
} //closes nested for loop
} //closes for loop
if (zeroCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 0's on the "
+ " sub-diagonal.");
} //closes if statement
if (oneCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 1's on the "
+ " sub-diagonal.");
} //closes if statement
if (twoCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 2's on the "
+ " sub-diagonal.");
} //closes if statement
if (threeCount >= 3) { //opens if statement
System.out.println("Got atleast three consecutive 3's on the "
+ " sub-diagonal.");
} //closes if statement
} //closes the subAnalysis method
} //closes class body
now my problem is when it finds one set of consecutive numbers on a line or column, it stops checking the rest and ends that method....why is that? why wont it check the other lines after the initial find? any ideas?
Re: URGENT!!!! help with array element comparing
Sorry, but nobody is going to want to read through all that code. If you want help, you'll have to boil it down to an SSCCE- just a few lines that demonstrates your specific question. Chances are, in the process of creating the SSCCE, you'll actually figure out the problem yourself.
PS- Putting things like "urgent" or "need help as soon as possible" actually decreases your chances of getting help. Everybody's question is urgent to them, so putting it in your title makes it seem like you think your time is more important than anybody else's.