卡住了如何进行文件输入

时间:2019-01-20 09:49:29

标签: file file-io adjacency-matrix main-method

我的任务是为有向图创建邻接矩阵 我有一个文本文件,例如显示图的逐行边缘 1 2 3 4 5 2 等等

我创建了两个类(Fileinput,Graph)和一个主要方法(Builder)。但是现在我坚持创建一种主方法,该方法可以读取文本并将其填充到图形中。另外,我不明白为什么我的图类无法获得我在FileInput类中创建的数组(Eclipse表示我应该创建一个局部变量)。此外,我在FileInput类中收到一个错误,我应该在末尾添加更多},但是我可以插入尽可能多的方括号,但仍然会收到此错误。

import java.io.File;
import java.util.Scanner;

public class FileInput{

    public int inputArray[][];
    public int numLines = 2772;
    public int numColumns = 2;

public FileInput(int numLines, int numColumns) {
    this.numLines = numLines;
    this.numColumns = numColumns;
    inputArray = new int [numLines][numColumns];
}

public void createArray() {
try {
        Scanner input = new Scanner(new File("Raw"));
        while (input.hasNextLine()) {
            for (int i = 0; i < numLines; i++) {
                for (int j = 0; j < numColumns; j++) {
                   try{//    System.out.println("number is ");
                    inputArray[i][j] = input.nextInt();
                     // System.out.println("number is "+ a[i][j]);
                    }
                   catch (java.util.NoSuchElementException e) {
                    e.printStackTrace();
                    }
                }
               //print the input matrix
//            System.out.println("The input sorted matrix is : ");
//            for (int i = 0; i < m; i++) {
//                for (int j = 0; j < n; j++) {
//                    System.out.println(inputarray[i][j]);

//                }
//            }
//        }
//    } catch (Exception e) {
//        e.printStackTrace();

         }
        }}}}



 public class Graph {

      public boolean adjMatrix[][];
      public int numVertices;
      public int checkMatrix[][];

 public Graph(int numVertices) {
          this.numVertices = numVertices;
          adjMatrix = new boolean[numVertices][numVertices];
          checkMatrix= new int[numVertices*numVertices][2];
    }

 public void fillCheckMatrix(int o) {
     for (int i = 0; i <numVertices; i++) {
         for (int j=0; i <numVertices; i++) {
         checkMatrix[i][j]=i;
     }}
 }

 public void addEdge(int i, int j) {
          for(int x = 0; x < checkMatrix.length; x++){
                //Iterate through all elements in input array    
                for(int m = 0; m < inputArray.length; m++){
                      if(checkMatrix[x][0] == inputArray[m][0] && checkMatrix[x][1] == inputArray[m][1]){
                          int i = x/numVertices;
                          int j = x%numVertices;
                          adjMatrix[x][j] = true;
                      }
                }}
      }

 public boolean isEdge(int i, int j) {
                  return adjMatrix[i][j];
    }

 public String toString() {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < numVertices; i++) {
            s.append(i + ": ");
            for (boolean j : adjMatrix[i]) {
                s.append((j?1:0) + " ");
            }
            s.append("\n");
        }
        return s.toString();}


    }
public class Builder {

public static void main(String[] args) {
    // TODO Auto-generated method stub
     {
            Graph g = new Graph(4);



        }
}

}

0 个答案:

没有答案