import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class floyd {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: java FloydApp <graphfile>");
            return;
        }

        String inputFile = args[0];
        try {
            File file = new File(inputFile);
            Scanner scanner = new Scanner(file);
            PrintWriter pw = new PrintWriter("output.txt");

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                
                if (line.startsWith("Problem") && scanner.hasNextLine()) {
                    // Skipping the "Amatrix:" line
                    scanner.nextLine();

                    // Reading the matrix size
                    line = scanner.nextLine().trim();
                    int n = Integer.parseInt(line.substring(line.indexOf("n =") + 3).trim());

                    // Initialize matrices
                    int[][] graph = new int[n][n];
                    int[][] path = new int[n][n];

                    // Read the graph matrix
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            if (!scanner.hasNextInt()) {
                                System.out.println("Error: Matrix data is incorrectly formatted or incomplete.");
                                scanner.close();
                                pw.close();
                                return;
                            }
                            int value = scanner.nextInt();
                            graph[i][j] = (i != j && value == 0) ? Integer.MAX_VALUE : value;
                            path[i][j] = -1;
                        }
                    }

                    // Apply Floyd’s algorithm
                    floydsAlgorithm(graph, path, n);

                    // Output the results
                    writeOutput(pw, graph, path, n);
                }
            }

            scanner.close();
            pw.close();
        } catch (Exception e) {
            System.out.println("Error processing the file: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void floydsAlgorithm(int[][] graph, int[][] path, int n) {
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (graph[i][k] == Integer.MAX_VALUE || graph[k][j] == Integer.MAX_VALUE) continue;
                    if (graph[i][j] > graph[i][k] + graph[k][j]) {
                        graph[i][j] = graph[i][k] + graph[k][j];
                        path[i][j] = k;
                    }
                }
            }
        }
    }

    private static void writeOutput(PrintWriter pw, int[][] dist, int[][] path, int n) {
        pw.println("Problem: n = " + n);
        pw.println("Pmatrix:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                pw.print((path[i][j] == -1 ? 0 : path[i][j] + 1) + " ");
            }
            pw.println();
        }

        // Generating paths from the path matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i != j) {
                    String pathStr = constructPath(i, j, path) + ": " + dist[i][j];
                    pw.println("V" + (i + 1) + "-V" + (j + 1) + ": " + pathStr);
                }
            }
        }
        pw.println(); // Blank line between problems
    }

    private static String constructPath(int i, int j, int[][] path) {
        if (path[i][j] == -1) {
            return "V" + (i + 1) + " V" + (j + 1);
        } else {
            return constructPath(i, path[i][j], path) + " " + constructPath(path[i][j], j, path).substring(3);
        }
    }
}
 i want this program to read input as Problem1 Amatrix: n = 7
0  6  5  4  6  3  6  
6  0  6  4  5  5  3  
5  6  0  3  1  4  6  
4  4  3  0  4  1  4  
6  5  1  4  0  5  5  
3  5  4  1  5  0  3  
6  3  6  4  5  3  0  

Problem2 Amatrix: n = 6
0  1  2  1  3  4  
1  0  3  2  2  3  
2  3  0  3  3  6  
1  2  3  0  3  5  
3  2  3  3  0  5  
4  3  6  5  5  0 and then give Output File
Output the solution of problem 1 first, then problem 2, and etc. The solution of problem j should start with an integer n (the number cities) and the n x n Pointer Array P (in the next n rows). The shortest paths should then follow, one per line. Output the shortest paths from C1 to all other cities, then C2 to all other cities, and Cn to all other cities. 

A sample output file:
Problem1: n = 7
Pmatrix: 
0  0  0  6  3  0  6  
0  0  5  0  0  4  0  
0  5  0  0  0  0  5  
6  0  0  0  3  0  6  
3  0  0  3  0  3  0  
0  4  0  0  3  0  0  
6  0  5  6  0  0  0 

V1-Vj: shortest path and length
V1 V1: 0
V1 V2: 6
V1 V3: 5
V1 V6 V4: 4
V1 V3 V5: 6
V1 V6: 3
V1 V6 V7: 6

V2-Vj: shortest path and length
V2 V1: 6
V2 V2: 0
V2 V5 V3: 6
V2 V4: 4
V2 V5: 5
V2 V4 V6: 5
V2 V7: 3

V3-Vj: shortest path and length
V3 V1: 5
V3 V5 V2: 6
V3 V3: 0
V3 V4: 3
V3 V5: 1
V3 V6: 4
V3 V5 V7: 6

V4-Vj: shortest path and length
V4 V6 V1: 4
V4 V2: 4
V4 V3: 3
V4 V4: 0
V4 V3 V5: 4
V4 V6: 1
V4 V6 V7: 4

V5-Vj: shortest path and length
V5 V3 V1: 6
V5 V2: 5
V5 V3: 1
V5 V3 V4: 4
V5 V5: 0
V5 V3 V6: 5
V5 V7: 5

V6-Vj: shortest path and length
V6 V1: 3
V6 V4 V2: 5
V6 V3: 4
V6 V4: 1
V6 V3 V5: 5
V6 V6: 0
V6 V7: 3

V7-Vj: shortest path and length
V7 V6 V1: 6
V7 V2: 3
V7 V5 V3: 6
V7 V6 V4: 4
V7 V5: 5
V7 V6: 3
V7 V7: 0

Problem2: n = 6
Pmatrix:
0  0  0  0  2  2  
0  0  1  1  0  0  
0  1  0  1  0  2  
0  1  1  0  0  2  
2  0  0  0  0  2  
2  0  2  2  2  0  

V1-Vj: shortest path and length
V1 V1: 0
V1 V2: 1
V1 V3: 2
V1 V4: 1
V1 V2 V5: 3
V1 V2 V6: 4

V2-Vj: shortest path and length
V2 V1: 1
V2 V2: 0
V2 V1 V3: 3
V2 V1 V4: 2
V2 V5: 2
V2 V6: 3

V3-Vj: shortest path and length
V3 V1: 2
V3 V1 V2: 3
V3 V3: 0
V3 V1 V4: 3
V3 V5: 3
V3 V1 V2 V6: 6

V4-Vj: shortest path and length
V4 V1: 1
V4 V1 V2: 2
V4 V1 V3: 3
V4 V4: 0
V4 V5: 3
V4 V1 V2 V6: 5

V5-Vj: shortest path and length
V5 V2 V1: 3
V5 V2: 2
V5 V3: 3
V5 V4: 3
V5 V5: 0
V5 V2 V6: 5

V6-Vj: shortest path and length
V6 V2 V1: 4
V6 V2: 3
V6 V2 V1 V3: 6
V6 V2 V1 V4: 5
V6 V2 V5: 5
V6 V6: 0