import java.util.Scanner;
public class ConsecutiveFour {
private final int MATCH_COUNT = 4;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = input.nextInt();
System.out.print("Enter number of columns: ");
int columns = input.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++){
matrix[row][column] = input.nextInt();
int value = matrix[row][column];
}
}
System.out.print(isConsecutiveFour(matrix));
}
public static boolean isConsecutiveFour(int[][] values) {
boolean cons = false;
int columns = values.length;
int rows = values[0].length;
//tests horizontally
for (int r=0; r < rows; r++) {
for (int c=0; c < columns - 3; c++){
if (values[c][r] == values[c+1][r] &&
values[c][r] == values[c+2][r] &&
values[c][r] == values[c+3][r]) {
cons = true;
}
}
}
//tests vertically
for (int r=0; r < rows - 3; r++) {
for (int c=0; c < columns; c++){
if (values[c][r] == values[c][r+1] &&
values[c][r] == values[c][r+2] &&
values[c][r] == values[c][r+3]) {
cons = true;
}
}
}
//tests diagonally (going down and to the right)
for (int r=3; r < rows; r++) {
for (int c=0; c < columns - 3; c++) {
if (values[c][r] == values[c+1][r-1] &&
values[c][r] == values[c+2][r-2] &&
values[c][r] == values[c+3][r-3]) {
cons = true;
}
}
}
//tests diagonally (going down and to the left)
for (int r=0; r < rows - 3; r++) {
for (int c=0; c < columns - 3; c++) {
if (values[c][r] == values[c+1][r+1] &&
values[c][r] == values[c+2][r+2] &&
values[c][r] == values[c+3][r+3]) {
cons = true;
}
}
}
return cons;
}
}
5 Replies - 106 Views - Last Post: 06 December 2012 - 02:01 PM
#1
Problem how to input "values of the list" into the program to
Posted 05 December 2012 - 11:27 PM
I am trying to run this program and I am stuck on how to input the values of the lists in rows and columns. The program is suppose to check whether a 2D list has 4 consecutive numbers of the same value, either horizontally, vertically or diagonally. I get an Exception error anytime I try to input values therefore I cant fully run the program.
Replies To: Problem how to input "values of the list" into the program to
#2
Re: Problem how to input "values of the list" into the program to
Posted 05 December 2012 - 11:31 PM
#3
Re: Problem how to input "values of the list" into the program to
Posted 05 December 2012 - 11:36 PM
Exception in thread "main" java.util.InputMistmatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at ConsecutiveFour.main(ConsecutiveFour.java:20)
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at ConsecutiveFour.main(ConsecutiveFour.java:20)
#4
Re: Problem how to input "values of the list" into the program to
Posted 06 December 2012 - 01:35 AM
Ok...I see what your problem is.
Try this...use input.nextLine() immediately after your input.nextInt()
What that does is that it not only takes your number, but also the \n character.
regards,
Raghav
Try this...use input.nextLine() immediately after your input.nextInt()
What that does is that it not only takes your number, but also the \n character.
regards,
Raghav
#5
Re: Problem how to input "values of the list" into the program to
Posted 06 December 2012 - 08:34 AM
Thank you that worked!! But it takes 14 lines of numbers before it outputs "false". Is there anyway I can change my code to fix this? Or am I inputing the "Values of Rows and Columns" wrong?
When "Enter the Rows and Columns" I go:
123456
123456
345566
546786
432545
321367
Then 14 lines liner through hitting enter i get a false for "No Four Consecutive numbers"
When "Enter the Rows and Columns" I go:
123456
123456
345566
546786
432545
321367
Then 14 lines liner through hitting enter i get a false for "No Four Consecutive numbers"
#6
Re: Problem how to input "values of the list" into the program to
Posted 06 December 2012 - 02:01 PM
123456 this is a single number that will be rea d by Scanner.nextInt() and will be store in one cell of your array not in the six columns of a row. You will have to enter
1 2 3 4 5 6
Also in your method to test... don't test everything more than once instead of
cons = true; // found
simply
return true; // nop need to test the rest
ad the end of the method, simply return false; if you haven't encounter a return true; before
1 2 3 4 5 6
Also in your method to test... don't test everything more than once instead of
cons = true; // found
simply
return true; // nop need to test the rest
ad the end of the method, simply return false; if you haven't encounter a return true; before
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|