0

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.

1 Answer 1

1

Okay. So the problem here results from a certain index of the 2D array being replaced over and over again by a new value. What I mean by that is: you have a while loop. You have set variables j and i to 0. But every iteration of the while loop, you are changing the same position in the 2D array; therefore, nothing gets changed. To fix this, we can make use of the first 2 numbers which detail the dimensions of the 2D array (in this case 5x5). We can then use those dimensions in a nested for-loop, and loop through the file and store each value into the 2D array.

The code could look something like this:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(new File("imagedata.txt"));

        int r = scanner.nextInt();
        int c = scanner.nextInt();
        int [][] array = new int[r][c];

        for(int row=0; row<array.length; row++) {
            for(int col=0; col<array[row].length; col++) {
                array[row][col] = scanner.nextInt();
            }
        }

        for(int[] row : array) {
            for(int num : row) {
                System.out.print(num+" ");
            }
            System.out.println();
        }


    }
}

Alternatively, if you don't mind using a 2d String array:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(new File("imagedata.txt"));

        int r = scanner.nextInt();
        int c = scanner.nextInt();
        scanner.nextLine();
        String [][] array = new String[r][c];

        for(int row=0; row<array.length; row++) {
            array[row] = scanner.nextLine().split(" ");
        }

        for(String[] row : array) {
            for(String str : row) {
                System.out.print(str+" ");
            }
            System.out.println();
        }


    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this got everything to print out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.