java - 使用 Java 从文件中读取值

标签 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(); 不起作用呢?

最佳答案

在编写了一些代码后,您正在读取第一个 int (3),然后尝试读取边缘数据,该数据读入 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 在读取时打印数据,以便您可以检查实际读取的内容,并使用调试器逐步执行代码,以便检查您的值变量更接近

关于java - 使用 Java 从文件中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36097555/

相关文章:

R,二项式分布图

java - Runnable 发布成功但没有运行

java - 如何将两个android studio项目合并为一个

C、检测有向图中的循环、DFS、不一致图

file - Gnuplot : Size of points

graph - Hadoop MapReduce 在图中实现最短路径,而不仅仅是距离

java - android ClassNotFoundException : com. sun.msv.datatype.xsd.XSDatatype

java - 为什么循环将 ArrayList 中添加的项目加倍

java - 我什么时候应该在 LinkedBlockingQueue 上使用 SynchronousQueue

Elasticsearch 和 X-Pack : how to get vertices/connections from nested documents