I'm working a project that requires me to create 2d arrays from an image data file and then sort said arrays into different formats based on values.
The sorting will come easy enough, but I'm running into an issue determining the size of an array from scanning the file.
The data of the file is formatted like so:
5 5
201 159 87 63 240
231 32 222 76 5
10 5 248 139 47
167 76 138 177 107
188 122 154 165 205
I need to use the first line to set the rows and columns of the array, but I can't figure out how to do so without scanning the rest of the data. Another thing, I need to be able to loop my code so that a file with multiple data sets in the displayed format can be read an placed into arrays.
Here's what I've come up with so far:
public static void main(String[] args) throws IOException {
File file = new File("imagedata.txt");
Scanner sc = new Scanner(file);
int i = 0;
int j = 0;
int[][] array = new int[i][j];
while (sc.hasNextInt()) {
i = sc.nextInt();
j = sc.nextInt();
array = array[i][j];
sc.nextline();
}
}
It's not much, but I've scrapped a lot of other drafts that got me nowhere. Any helpful advice is welcome.