开发者

Finding Connected Components and reading an adjacency matrix

开发者 https://www.devze.com 2023-04-09 01:38 出处:网络
This is actually my first post here. I wasn\'t aware of this website, and I lurked around the questions and its certainly a place I want to keep coming to.

This is actually my first post here. I wasn't aware of this website, and I lurked around the questions and its certainly a place I want to keep coming to.

I'm in my Senior year for Computer Science but my programming skills are not that good, which makes me feel very disappointed in myself.

Anyways, I have a project due on Friday and I've been working on it since Saturday and I keep bashing my head making no progress.

I need to find the connected components in a graph and also read from a file an adjacency matrix.

I first tried in in python, using an igraph python library but I just kept having system errors. So after two days of trying to troubleshoot that, I gave up and moved onto Java where I'm trying to read the array.

Here is my current code that I have thus far, and I'm trying to google the best I can to find the answer. Right now I'm simply trying to read from a file and put the values into my 2d array. I commented out most of everything because I'm trying to simply figure out what I'm doing wrong.

package javaapplication1;
import java.io.*;

import java.util.Scanner;
import tio.*;


public class JavaApplication1 {


 public static void adjMatrix() throws FileNotFoundException, IOException{
   int i, j, n = 20;
   int[][]array = new int[n][n];
   String file = ("adjmatrix.txt");

   BufferedReader in = new BufferedReader(new FileReader(file));
   System.out.println(in.readLine());
   in.close();
   /*
   while(in.hasMoreElements()){
       for (i = 0; i < n; i++){
          for (j = 0; j < n; j++){
           array[i][j] = in.readInt();
          }    // end inner for     
        } // end outer for
   }

   //Print array
   System.out.println("Here is the matrix: ");
   for (i = 0; i < n; i++){
       for (j = 0; j < n; j++){
           System.out.print(array[i][j]);
       } // end innerfor
   } //end outerfor
   */





} // endclass



/**
 * @param args the command line arguments
 */
public static void main(String[] args)
    throws IOException{
    adjMatrix();
} // end main
} // end class
#

ERROR: Exception in开发者_运维问答 thread "main" java.io.FileNotFoundException: adjmatrix.txt

(The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:97) at java.io.FileReader.(FileReader.java:58) at javaapplication1.JavaApplication1.adjMatrix(JavaApplication1.java:26) at javaapplication1.JavaApplication1.main(JavaApplication1.java:60) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

#


When you try to open a file without a direct path, Java will try to resolve it from the relative path. Here, since you simply put "adjmatrix.txt", it assumes that the text file is living in the same directory as the class file with the main method that you execute (JavaApplication1 in package JavaApplication1). You should either move the text file into the package right next to the class file or add a more specific path name as Kevin stated.

0

精彩评论

暂无评论...
验证码 换一张
取 消