从Java中读取文件中的值

时间:2016-03-19 03:43:00

标签: java graph

所以我建立一个图表,使用Warshall算法找到两点之间的最短距离。这是我的代码的一小部分:

public Edge( In in ) {
        System.out.println("here1");
        String location = in.readString();
        System.out.println("here1b");
        int V = in.readInt();
        System.out.println("here1c");
        dist = new int [V][V];
    System.out.println("here1d");
    int n = in.readInt();

以下是输入:

3
4 
A 0 0 0 
B 5 0 0 
C 5 5 0 
D 0 5 0 
2 
A C
C D

现在输出看起来像这样。

case 1:
here1
here1b
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at edu.princeton.cs.algs4.In.readInt(In.java:322)
    at Edge.<init>(Edge.java:10)
    at Edge.main(Edge.java:51)

所以当我in.readInt();时会发生异常。这是我的主要方法:

 public static void main(String[] args) {
      In in = new In( args[0] );
      int T = in.readInt();
      for (int t=1; t<=T; t++) {
         System.out.println("Case " + t + ":") ;
         Edge w = new Edge( in );
         int Q = in.readInt();
         for (int i=0; i<Q; i++) {
            String p1s = in.readString();
            String p2s = in.readString();
         }
      }

这是我的全部边缘课程:

public Edge( In in ) {
    String location = in.readString();
    System.out.println(location);
    for (int index = 0; index < 3; index++) {
        System.out.println(in.readInt());
        dist = new int [V][V];
        int n = in.readInt();
        int [][] G = new int [n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++){
                dist[i][j] = in.readInt();
            }
        }
        }

        /*int E = in.readInt();
        for (int l = 0; l < E; l++){
            int i = in.readInt();
            int j = in.readInt();
        }
        */
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
       }

这就是我在In课程中使用的内容:http://algs4.cs.princeton.edu/12oop/In.java.html。那么in.readInt();怎么没有用呢?

1 个答案:

答案 0 :(得分:1)

在玩了一些代码之后,您正在阅读第一个int3),然后尝试读取边缘数据,该数据读入4A,但由于A不是int,而且会中断。

如果我将您的代码更改为更像......

public static void main(String[] args) {
    In in = new In("Data.txt");
    int T = in.readInt(); // 3
    int something = in.readInt(); // 4
    for (int t = 1; t <= T; t++) {
        System.out.println("Case " + t + ":");
        Edge w = new Edge(in);
    }
}

public class Edge {

    public Edge(In in) {
        String location = in.readString();
        System.out.println(location);
        for (int index = 0; index < 3; index++) {
            System.out.println(in.readInt());
        }
    }

}

打印出类似......的内容。

Case 1:
A
0
0
0
Case 2:
B
5
0
0
Case 3:
C
5
5
0

我没有足够的信息来了解文件的所有结构以完全纠正您的代码,但您需要确保正确阅读它。您还应该在阅读时使用System.out.println打印数据,这样您就可以检查您实际阅读的内容以及调试器以逐步执行代码,以便您可以更密切地检查变量的值