-2

I'm supposed to take the first two integers from a txt file and create an array using them. Here is the txt file that was given to me.

5 is how many rows are in the array. 6 is how many columns are in the array. I started off with just trying to read the first integer but when I try to run it I get an error. enter image description here

5
6
68 72 75 81 72 75
92 87 71 83 60 75
90 52 13 63 13 61
84 67 100 99 98 92
51 32 94 76 55 11
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class main {

    public static void main(String[] args) throws FileNotFoundException {
        File fileToRead = new File("array.txt");
        Scanner input = new Scanner(fileToRead);
        int grades = input.nextInt();
        int[][] array = new int[grades][grades];

        while (input.hasNext()) {
            for (int row = 0; row < grades; row++) {
                for (int column = 0; column < grades; column++) {
                    array[row][column] = input.nextInt();
                }
            }
        }
        input.close();

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.println(array[i][j]);
            }
            System.out.println();
        }
    }
}

I'm not sure if I'm taking the right approach but this is what I have so far.

pnrgl
  • 7
  • 2

1 Answers1

0

Good job! Your code is close to correct. The problem is that you aren't reading the second count, 6, and so you are reading 5 * 5 = 25 integers from the file to fill out your array. After doing this, there are still 5 numbers left to be read. Because of this, and the line while (input.hasNext()), you then start over again and try to read another 25 numbers from the file. This is what gets you into trouble, as there are not 25 more numbers to read. If you take out the while (input.hasNext()) line, your code will run. If you read and use the 6, your code should do exactly what you're hoping it will.

Here's a version of your code with just minor fixes that does the right thing:

public static void main(String[] args) throws FileNotFoundException {
    File fileToRead = new File("/tmp/data.txt");
    Scanner input = new Scanner(fileToRead);
    int rows = input.nextInt();
    int cols = input.nextInt();
    int[][] array = new int[rows][cols];

    for (int row = 0; row < rows; row++) {
        for (int column = 0; column < cols; column++) {
            array[row][column] = input.nextInt();
        }
    }
    input.close();

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            System.out.print(array[i][j]);
            System.out.print(' ');
        }
        System.out.println();
    }
}

Result:

68 72 75 81 72 75 
92 87 71 83 60 75 
90 52 13 63 13 61 
84 67 100 99 98 92 
51 32 94 76 55 11 
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • OMG! I just edited my code to look similar to yours literally a minute ago. The only difference is adding the space after each value. A question I have now is, and is something I am confused with, this is supposed to be printed all on another class I was just writing it in my main class for testing purposes. How would I go about doing so? – pnrgl Oct 25 '22 at 03:57
  • There are many examples of how to do this. Just google "java class example". Here's one that I found right at the top that I think demonstrates what you need to know: https://www.w3schools.com/java/java_classes.asp – CryptoFool Oct 25 '22 at 04:23
  • Actually, that one seems kinda lame. Here's a better one: https://www.programiz.com/java-programming/class-objects – CryptoFool Oct 25 '22 at 04:25
  • So what we did was correct actually, I have another class with a printArray() method, how do I accept the 2D array in this method ? – pnrgl Oct 25 '22 at 05:21
  • Just define the method as taking `int[][] array`. You can get the sizes with `array.length` and array[0].length` – CryptoFool Oct 25 '22 at 06:33