Problem how to input "values of the list" into the program to

Page 1 of 1

5 Replies - 699 Views - Last Post: 06 December 2012 - 02:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302927&amp;s=c53f88935f74bb8d3d9740cde57b546a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 abbey21

Reputation: 0
• Posts: 18
• Joined: 05-December 12

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.

```
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;
}

}
```

Is This A Good Question/Topic? 0

Replies To: Problem how to input "values of the list" into the program to

#2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

Re: Problem how to input "values of the list" into the program to

Posted 05 December 2012 - 11:31 PM

abbey21, on 06 December 2012 - 11:57 AM, said:

I get an Exception error anytime I try to input values therefore I cant fully run the program.

Can you please tell us what is the exact error you get so that it will be helpful to find out where your program is throwing that exception.

regards,
Raghav

#3 abbey21

Reputation: 0
• Posts: 18
• Joined: 05-December 12

Re: Problem how to input "values of the list" into the program to

Posted 05 December 2012 - 11:36 PM

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 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

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

#5 abbey21

Reputation: 0
• Posts: 18
• Joined: 05-December 12

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"

#6 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

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