Moja porada skorzystać z file - path. Bez odczytu scannerem lini skoro masz plik, to powinieneś wczytać plik
int[][] turnFileIntoDataBase(Path filePath) throws IOException {
//zalecam obsluge try with resources,
//dla zapewnienia gwarancji odczytu lub blok finally
return Files.lines(filePath)
.map((fileLine) -> fileLine.trim()
.split(","))
.map((eachLineNumbers) -> Stream.of(eachLineNumbers)
.mapToInt(Integer::parseInt)
.toArray())
.toArray(int[][]::new);
}
int bazadanych[][] = turnFileIntoDataBase(Paths.get("pathToSource/przyklad.txt"));
Tu masz różnice Path vs File
https://www.baeldung.com/java-path-vs-file
Path fileFromPath = Paths.get("pathToSource/przyklad.txt");
Dla pliku z danymi :
1,2,3,4,5,6,7
5,6,7,8,9,11,21
i przykładowym rozwiązaniu ( deepToString, po prostu mapuje mi do wyświetlenia na szybko, aby pokazać wynik )
public static void main(String[] args) throws IOException {
System.out.println(Arrays.deepToString(turnFileIntoDataBase(Paths.get(
"numerki.txt"))));
}
wynik programu :
[
[1, 2, 3, 4, 5, 6, 7],
[5, 6, 7, 8, 9, 11, 21]
]
wyświetli Ci odpowiednie dane, dodatkowo możesz odwołać się bezpośrednio do elementów.
final int[][] ints = turnFileIntoDataBase(Paths.get(
"numerki.txt"));
System.out.println("arr" + ints[1][0]);
// 2 rekord, 1 element [ liczba 5]