Lesen einer Datei in ein mehrdimensionales Array
-
09-10-2019 - |
Frage
Ich möchte in einem Raster von Zahlen (n * n) aus einer Datei und kopieren Sie sie in ein mehrdimensionales Array, ein int zu einem Zeitpunkt, zu lesen. Ich habe den Code in der Datei zu lesen und ausdrucken, aber nicht wissen, wie die einzelne int zu nehmen. Ich denke, ich muß split Verfahren und eine leere Trenn „“, um jeden charcter zu nehmen, aber nach, dass im nicht sicher. Ich mag auch Leer auf 0 ändern, aber das kann warten!
Dies ist, was ich bisher habe, obwohl es nicht funktioniert.
while (count <81 && (s = br.readLine()) != null)
{
count++;
String[] splitStr = s.split("");
String first = splitStr[number];
System.out.println(s);
number++;
}
fr.close();
}
Eine Beispieldatei ist wie folgt (die Räume sind erforderlich):
26 84
897 426
4 7
492
4 5
158
6 5
325 169
95 31
Im Grunde weiß ich, wie in der Datei zu lesen und ausdrucken, aber nicht wissen, wie die Daten von dem Leser zu nehmen und es in einem mehrdimensionalen Array setzen.
Ich habe das gerade versucht, aber es sagt, 'kann von String nicht covernt [] zu String'
while (count <81 && (s = br.readLine()) != null)
{
for (int i = 0; i<9; i++){
for (int j = 0; j<9; j++)
grid[i][j] = s.split("");
}
Lösung
Based on your file this is how I would do it:
Lint<int[]> ret = new ArrayList<int[]>();
Scanner fIn = new Scanner(new File("pathToFile"));
while (fIn.hasNextLine()) {
// read a line, and turn it into the characters
String[] oneLine = fIn.nextLine().split("");
int[] intLine = new int[oneLine.length()];
// we turn the characters into ints
for(int i =0; i < intLine.length; i++){
if (oneLine[i].trim().equals(""))
intLine[i] = 0;
else
intLine[i] = Integer.parseInt(oneLine[i].trim());
}
// and then add the int[] to our output
ret.add(intLine):
}
At the end of this code, you will have a list of int[]
which can be easily turned into an int[][]
.
Andere Tipps
private static int[][] readMatrix(BufferedReader br) throws IOException {
List<int[]> rows = new ArrayList<int[]>();
for (String s = br.readLine(); s != null; s = br.readLine()) {
String items[] = s.split(" ");
int[] row = new int[items.length];
for (int i = 0; i < items.length; ++i) {
row[i] = Integer.parseInt(items[i]);
}
rows.add(row);
}
return rows.toArray(new int[rows.size()][]);
}
EDIT: You just updated your post to include a sample input file, so the following won't work as-is for your case. However, the principle is the same -- tokenize the line you read based on whatever delimiter you want (spaces in your case) then add each token to the columns of a row.
You didn't include a sample input file, so I'll make a few basic assumptions.
Assuming that the first line of your input file is "n", and the remainder is the n x n integers you want to read, you need to do something like the following:
public static int[][] parseInput(final String fileName) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
int n = Integer.parseInt(reader.readLine());
int[][] result = new int[n][n];
String line;
int i = 0;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split("\\s");
for (int j = 0; j < n; j++) {
result[i][j] = Integer.parseInt(tokens[j]);
}
i++;
}
return result;
}
In this case, an example input file would be:
3
1 2 3
4 5 6
7 8 9
which would result in a 3 x 3 array with:
row 1 = { 1, 2, 3 }
row 2 = { 4, 5, 6 }
row 3 = { 7, 8, 9 }
If your input file doesn't have "n" as the first line, then you can just wait to initialize your final array until you've counted the tokens on the first line.