I know this has been asked before, but I'm still confused after reading other's posts. I'm trying to read a cost adjacency matrix from an external file and use it to populate my 2d array which I'll end up printing. I'm able to read the information in the text file, but my matrix isn't filling in properly. Right now it looks like this:
What I'm shooting for would look like this,
New York Boston Atlanta St.Paul
New York 0 120 400 -1
Boston 120 0 1700 500
Atlanta 400 1700 0 -1
St.Paul -1 500 -1 0
Here is my code so far:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Lab12MU2 {
public static void main(String[] args) {
String fileName;
FileReader myFileRead;
String[][] matrix;
boolean populated = false;
Scanner scn = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
fileName = scn.nextLine();
try {
myFileRead = new FileReader(fileName);
BufferedReader buffRead;
buffRead = new BufferedReader(myFileRead);
Scanner input = new Scanner(new File(fileName));
int dim = 5;
matrix = new String[dim][dim];
int i, j;
for (i = 0; i < dim; i) {
for (j = 0; j < dim - 1; j) {
if (input.hasNext()) {
matrix[i][j] = input.nextLine();
}
}
}
for (i = 0; i < matrix.length; i ) {
for (j = 0; j < matrix[0].length; j ) {
System.out.print(matrix[i][j] "\t");
}
System.out.println();
}
populated = true;
myFileRead.close();
}
catch (Exception ex) {
System.out.println("ERROR: File not found");
}
}
}
Any help in fixing my matrix to look anything close to my desired result is appreciated. Also, here's the text file: https://www.dropbox.com/s/owikpr2vqx31kjn/CAM.txt?dl=0
New York, Boston, Atlanta, St. Paul
0, 120, 400, -1
120, 0, 1700, 500
400, 1700, 0, -1
-1, 500, -1, 0
CodePudding user response:
Notes after the code.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Lab12MU2 {
public static void main(String[] args) {
Path path = Paths.get("CAM.txt");
try (Stream<String> lines = Files.lines(path)) {
List<String[]> list = lines.map(line -> line.split(", "))
.collect(Collectors.toList());
String[][] matrix = list.toArray(new String[][]{});
boolean first = true;
for (int col = 0; col < matrix[0].length; col ) {
if (first) {
first = false;
}
else {
System.out.print(" ");
}
System.out.print(matrix[0][col]);
}
System.out.println();
for (int row = 1; row < matrix.length; row ) {
first = true;
for (int col = 0; col < matrix[row].length; col ) {
if (first) {
first = false;
}
else {
System.out.print(" ");
}
System.out.printf("%" matrix[0][col].length() "s", matrix[row][col]);
}
System.out.println();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Above code assumes that the file name is CAM.txt rather than getting the file name from the user. Note that, according to the above code, the file must be in the working directory.
The code uses the stream API and try-with-resources.
Refer to the documentation for method printf
.
When I run the above code, using the file from your question, I get the following output:
New York Boston Atlanta St. Paul
0 120 400 -1
120 0 1700 500
400 1700 0 -1
-1 500 -1 0