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
NandINFINITYjust to see ifnextLine()ends up skipping any line(s).ifs are not really necessary, you have zeroes in the diagonal already, andif(x==42)x=42;-constructs are not very useful.