问:错误:线程“ main”中的异常java.util.InputMismatchException

时间:2019-11-18 22:53:01

标签: java java.util.scanner dynamic-programming inputmismatchexception floyd-warshall

我正在编写一个程序,使用Floyd-Warshall算法解决所有对最短路径问题。我得到了一个txt格式的测试文件,其中第一个int是顶点数,第二个int是无穷大,其余的是包含长度信息的矩阵。这是我的测试文件和我编写的Java代码。

3
999
0  16 999
3  0  18
12 1  0
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class FloydWarshall
{
    private int adj[][];
    private int N;

    public FloydWarshall(int N)
    {
        adj = new int[N][N];
        this.N = N;
    }

    public void floydwarshall(int adj[][])
    {
        // i = source vertex
        // j = destination vertex
        // k = intermediate vertex
        int dist[][] = new int[N][N];

        // Initial values of shortest distances based on shortest paths
        // considering no intermediate vertex.
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dist[i][j] = adj[i][j];
            }
        }

        // If vertex k is on the shortest path from i to j, update the value of dist[i][j]
        for (int k = 0; k < N; k++)
        {
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (dist[i][k] + dist[k][j] < dist[i][j])
                        dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }

        // Print the result
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                System.out.print(dist[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static void main(String args[])
            throws FileNotFoundException {

        int adj[][]; // Adjacency matrix
        int N; // Number of vertices
        final int INFINITY; // Define infinity value

        File testFile = new File("src/test.txt"); // Enter file pathname
        Scanner inFile = new Scanner(testFile);

        // Read first int on first line as the number of vertices
        N = inFile.nextInt();
        // Read first int on second line as the infinite value
        INFINITY = inFile.nextInt();

        // System.out.println(N);
        // System.out.println(INFINITY);

        // An adjacency matrix to store input from the rest of the file
        adj = new int[N][N];

        // Input the matrix
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                adj[i][j] = inFile.nextInt();
            }
        }

        // Print Solution
        System.out.println("The matrix of shortest path distances is:");
        FloydWarshall f = new FloydWarshall(N);
        f.floydwarshall(adj);
        inFile.close();
    }
}

当我尝试从txt文件输入数据时,我不断收到此错误。如果我只是通过键盘手动输入所有数据,我的代码就可以正常工作。

  

线程“主”中的异常java.util.InputMismatchException在   java.base / java.util.Scanner.throwFor(Scanner.java:939)在   java.base / java.util.Scanner.next(Scanner.java:1594)位于   java.base / java.util.Scanner.nextInt(Scanner.java:2258)在   java.base / java.util.Scanner.nextInt(Scanner.java:2212)在   FloydWarshall.main(FloydWarshall.java:81)

我在上一个项目中遇到了相同类型的错误,所以我对如何从txt文件输入数据感到困惑。

我删除了测试文件中的多余空格,代码终于起作用了。

3
999
0 16 999
3 0 18
12 1 0

0 个答案:

没有答案