将读取文件中的整数字符存储到二维数组中

时间:2014-03-12 22:57:55

标签: java arrays

我试图编写一个读取数组文件的程序(以Rows作为第一个字符排列,Columns作为下一个字符,然后是一盒RxC术语)并尝试确定如果水平,垂直或对角线两个相邻的五个字符相同,则颜色不同(在我的GUI主程序中)

代码非常慢,只适用于较小的数组?我不明白我做错了什么。

文件看起来像这样:

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

代码:

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

当我计算7x7时,我得到打开和处理的确认给出了所有零的数组(7x7)。 当我计算15x14时,我在线程&#34; AWT-EventQueue-0&#34;中得到&#34;异常。错误,处理时没有数组。

2 个答案:

答案 0 :(得分:1)

一些建议:

  1. 创建一个返回int [] []的方法以读入输入文件
  2. 读取行和列后,创建矩阵int [] [] result = new int [row] [column];
  3. 在你的循环中,将每个整数读入矩阵(result [i] [j] = scan.nextInt();
  4. 在扫描一行'

    上的所有数字后,不要忘记移动到下一行

    您可以使用以下内容:

    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();
        }
    }
    }
    

答案 1 :(得分:0)