import java.io.*;
import java.util.Scanner;

public class floyd {

    private static final int INF = (int) 1e9; // Assuming this might be used for initialization

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

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

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.contains("Problem")) {
                    processInput(line, scanner, writer);
                }
            }

            scanner.close();
            writer.close();

        } catch (FileNotFoundException e) {
            System.err.println("File not found: " + e.getMessage());
        }
    }

    private static void processInput(String line, Scanner scanner, PrintWriter writer) {
        // Initial splitting to identify the problem number
        String[] initialParts = line.split(" ");
        int problemNumber;
        try {
            // Assuming the line starts with "ProblemX" where X is the problem number
            problemNumber = Integer.parseInt(initialParts[0].replaceAll("[^0-9]", ""));
        } catch (NumberFormatException e) {
            System.err.println("Error parsing problem number from line: " + line);
            return; // Skip this problem due to parsing error
        }
    
        // Already read the line containing "Amatrix:", no need to skip
        // Directly extract "n"
        String nText = initialParts[2]; // Based on the assumption it’s at the third position
        int n = Integer.parseInt(nText);
    
        // Initialize distance and path matrices with appropriate values
        int[][] dist = new int[n][n];
        int[][] P = new int[n][n];
    
        // Read n x n matrix values
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (scanner.hasNextInt()) { // Added check to ensure there’s an integer to read
                    dist[i][j] = scanner.nextInt(); // Fill the distance matrix
                    P[i][j] = 0; // Initialization implying no intermediate vertex yet
                }
            }
        }

        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];
                        P[i][j] = k+1; // Store intermediate vertex
                    }
                }
            }
        }

        writer.printf("Problem %d: n = %d\n", problemNumber, n);
        writer.println("Pmatrix:");
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                writer.print(P[i][j] + " ");
            }
            writer.println();
        }

        for(int i = 0; i < n; i++) {
            writer.printf("V%d-Vj: shortest path and length\n", i+1);
            for(int j = 0; j < n; j++) {
                displayPath(i, j, P, writer, "");
                writer.printf(": %d\n", dist[i][j]);
            }
        }
        writer.println();
    }

    private static void displayPath(int i, int j, int[][] P, PrintWriter writer, String path) {
        if(P[i][j] == 0) {
            if(!path.contains("V" + (j+1))) {
                writer.print(path + "V" + (j+1) + " ");
            }
            return;
        }
        displayPath(i, P[i][j]-1, P, writer, path);
        displayPath(P[i][j]-1, j, P, writer, path);
    }

} java floyd graphfile.txt
Exception in thread "main" java.lang.NumberFormatException: For input string: "n"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
        at java.base/java.lang.Integer.parseInt(Integer.java:662)
        at java.base/java.lang.Integer.parseInt(Integer.java:778)
        at floyd.processInput(floyd.java:49)
        at floyd.main(floyd.java:22)