5 Replies - 387 Views - Last Post: 06 December 2012 - 02:01 PM Rate Topic: -----

#1 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

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

Posted 05 December 2012 - 11:31 PM

View Postabbey21, 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
Was This Post Helpful? 0
  • +
  • -

#3 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

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)
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 abbey21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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"
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1