0

I am writing a program to solve the all-pairs shortest paths problem using the Floyd-Warshall algorithm. I am given a test file in txt format in which the first int is the number of vertices, the second int is the value of infinity and the rest is a matrix containing length information. This is my test file and the Java code I wrote.

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();
    }
}

I constantly get this error when I tried to input the data from txt file. My code works fine if I just manually input all the data via keyboard.

Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at FloydWarshall.main(FloydWarshall.java:81)

I had the same type of error for my last project, so I am so confused about how to input data from txt file.

I deleted the extra spaces in my test file and the code finally worked.

3
999
0 16 999
3 0 18
12 1 0
6
  • 1
    You may want to print N and INFINITY just to see if nextLine() ends up skipping any line(s). Commented Nov 18, 2019 at 22:58
  • Side comment: those ifs are not really necessary, you have zeroes in the diagonal already, and if(x==42)x=42;-constructs are not very useful. Commented Nov 18, 2019 at 23:03
  • @tevemadar thanks for the suggestion, I deleted both if. It appears that nextLine is also unnecessary and I deleted them. The scanner successfully inputted the first two int in the test file but I am not sure about the for-loop that inputs the matrix and the error is still there Commented Nov 18, 2019 at 23:11
  • 2
    Print every int that you read, and you'll know where the non-integer token is present in your file. Commented Nov 18, 2019 at 23:13
  • Thanks all, it turns out I can only have 1 space between each two int for the matrix in my test file, otherwise, it wouldn't input. Commented Nov 19, 2019 at 5:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.