Arpit Patel
Arpit Patel

Reputation: 8047

Read text file and make a 2D array

I want to read a text file and make a 2D array from it and I am not familiar with the 2D array. SO any help much appreciated My text file contains all the numbers.

I also tried with for loop but I was getting the same error.

Please review my code effort here

  Scanner readFile = new Scanner(new File("ELEVATIONS.txt"));
            rowLength = readFile.nextInt();
            colLength = readFile.nextInt();
            Radius = readFile.nextInt();

            int[][] data = new int[rowLength][colLength];

            System.out.println("ROW : " + rowLength + " COl : " + colLength + " Radius : " + Radius );

            int row = 0;
            while (readFile.hasNextInt() && row < data.length) {
                for (int col = 0; col < colLength; col++) {
                    data[row][col]= readFile.nextInt();
                }
                row++;
            }

When I run the code it gives me below Error

ROW : 1000 COl : 450 Radius : 10 data 1000450 Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at lab1.Lab1.main(Lab1.java:43)

Please help me.

Upvotes: 0

Views: 108

Answers (3)

GhostCat
GhostCat

Reputation: 140407

Your "2D array" code looks fine, you prepare the "empty" array, and you loop according to its dimensions.

The essence of your problem manifests here:

java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at

The point is: while reading the file you end up with an error. That has nothing to do whether you would only print the values to the console, or whether you store them in that pre-defined array.

In other words: at this point, your problem is that your file content does not match your expectations/assumptions. Thus you ask for a number ... where there is none.

The answer: step back. Start with a really small file that only contains say 2 2 5 1 2 3 4. Now, run your code with that, and then print every relevant information (for example the value read from the file). So that you understand what exactly your code is doing.

In other words: debugging is required. Either by actually using a debugger, or by adding print statements. You need to be able to observe your code doing its work.

Upvotes: 0

Shubham Chopra
Shubham Chopra

Reputation: 1727

The error you are getting is mostly because your text file does not contain the element your code is trying to fetch.

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

public class Test {
    public static void main(String[] args) throws Exception {
        // pass the path to the file as a parameter
        File file =

                new File("C:\\Users\\LENOVO\\Documents\\test.txt");
        Scanner sc = new Scanner(file);
        int rowLength = 0;
        int colLength = 0;
        int radius = 0;

        rowLength = sc.nextInt();
        colLength = sc.nextInt();
        radius = sc.nextInt();

        int[][] data = new int[rowLength][colLength];
        System.out.println("ROW : " + rowLength + " COl : " + colLength + " Radius : " + radius);

        for (int i = 0; i < rowLength; ++i) {
            for (int j = 0; j < colLength; ++j) {
                if (sc.hasNextInt()) {
                    data[i][j] = sc.nextInt();
                }
            }
        }

        // printing the array
        for (int i = 0; i < rowLength; i++) {
            for (int j = 0; j < colLength; j++) {
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
    }
}

enter image description here

enter image description here My text file content- 2 2 5 1 2 3 4

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

I think that the simplest way is to read all integers from a file and fill 2D array using counters:

public static int[][] readFile(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        int rows = scan.nextInt();
        int cols = scan.nextInt();

        int[][] res = new int[rows][cols];

        for (int row = 0, col = 0; row < rows; ) {
            res[row][col++] = scan.nextInt();

            if (col == cols) {
                row++;
                col = 0;
            }
        }

        return res;
    }
}

Upvotes: 1

Related Questions