Reputation: 53
I'm trying read a file and store the numbers into a 2D array. I'm using:
int lines = (int) Files.lines(Paths.get(filePath)).count();
to get the total amount of lines in the file to create my 2D array, the problem is i cant find a way to check if a line has some extra numbers.
67,1,43
13,37,61,3
31,73,7
With the example above the program will work just fine but I want to stop if the numbers of each line aren't even. Is there a specific way to count the elements of one line and compare them to the elements of the other lines?
Upvotes: 0
Views: 90
Reputation: 5251
You can split the lines, check the array length and just store the maximum length somewhere.
For example:
int maxLength = 0;
Files.lines(path).forEach(line -> {
int lineLength = line.split(",").length;
if(lineLength > maxLength) {
maxLength = lineLength;
}
});
Upvotes: 1