2

I'm trying to write a program that reads a file of an array (arranged with the Rows as the first character, and the Columns as the next character, and then a box of RxC terms) and tries to determine if five characters next to each other horizontally, vertically, or either way diagonally are the same, to color differently (in my GUI main program)

The code is EXTREMELY slow, and only works for smaller arrays? I don't understand what I'm doing wrong.

The Files look like this:

 5 4
 1 2 3 4 5
 1 2 3 4 5
 7 3 2 0 1
 6 1 2 3 5   

Code:

public class fiveinarow
{
  int[][] Matrix = new int [100][100];
  byte[][] Tag = new byte [100][100];
  int row, col;
  String filepath, filename;

  public fiveinarow()
  {
    row = 0;
    col = 0;
    filepath = null;
    filename = null;
  }

  public void readfile()
  {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogType(JFileChooser.OPEN_DIALOG );
    chooser.setDialogTitle("Open Data File");

    int returnVal = chooser.showOpenDialog(null);
    if( returnVal == JFileChooser.APPROVE_OPTION) 
    {
      filepath = chooser.getSelectedFile().getPath();
      filename = chooser.getSelectedFile().getName();
    }

    try 
    {
            Scanner inputStream  = new Scanner(new FileReader(filepath));
        int intLine;
    row = scan.nextInt();
    col = scan.nextInt();
    for (int i=0; i < row; i++)
            {
        for (int j = 0 ; j < col; j++)
        {
                        int[][]Matrix = new int[row][col];
            Matrix[i][j] = inputStream.nextInt();
                    }
    }
}


    catch(IOException ioe)
    {
      System.exit(0);
    }
  }

When I compute a 7x7, I get confirmation of opening and processing gives an array (7x7) of all Zeroes. When I compute a 15x14, I get "Exception in thread "AWT-EventQueue-0" errors and no array when processed.

1
  • You appear to have rows and columns mixed (if your example data is correct data) . your example has four rows and five columns Commented Mar 13, 2014 at 0:07

2 Answers 2

1

Some suggestions:

  1. Create a method that returns int[][] to read in your input file
  2. After reading the row and column, create the matrix int[][] result = new int[row][column];
  3. inside your loop, read each integer into the matrix (result[i][j] = scan.nextInt();

Don't forget to move to the next line after scanning all the numbers on one line'

You might use something like:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class ReadMatrix {
static ReadMatrix mReadMatrix;
int[][] matrix;
int row, col;
String filepath, filename;

/**
 * @param args
 */
public static void main(String[] args) {
    mReadMatrix = new ReadMatrix();
    mReadMatrix.readfile();
}

// int[][] Matrix = new int [100][100];
// byte[][] Tag = new byte [100][100];


public void readfile() {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setDialogTitle("Open Data File");

    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        filepath = chooser.getSelectedFile().getPath();
        filename = chooser.getSelectedFile().getName();
    }

    Scanner inputStream;
    try {
        inputStream = new Scanner(new FileReader(filepath));
        row = inputStream.nextInt();
        col = inputStream.nextInt();
        System.out.println(" matrix is " + row + " rows and " + col + " columns");
        matrix = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                matrix[i][j] = inputStream.nextInt();
                System.out.println(" " + i + "," + j + ": " + matrix[i][j]);
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

I revised the 'try' to int[][]Matrix = new int[row][col]; Matrix[i][j] = inputStream.nextInt(); But it gives me a array of all 0's and doesn't work for larger file sizes?
Could you give me an example using the code? I don't think I fully understand.
The code outputs to the system, when I need it to be in the gui and passed to a driver that redraws the array.
0

You might want to check out something similar that I did: Loading Tile Maps From Text Files In Slick2D

or Why Aren't My Tile Maps Displaying Correctly?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.