Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading numbers from a .txt file into a 2d array and print it on console

So basically i am trying to read a .txt file that contains these following in it:

3 5 

2 3 4 5 10

4 5 2 3 7

-3 -1 0 1 5

and store them into 2D array and print it on console, what i got from the console is fine but only missing the first row 3 5, which i do not know what is wrong with my code made it ignored the first line. what i got as output now:

2  3  4  5 10 

4  5  2  3  7

-3 -1  0  1  5 

import java.io.*;
import java.util.*;

public class Driver0 {
    public static int[][] array;
    public static int dimension1, dimension2;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to Project 0.");
        System.out.println("What is the name of the data file? ");
        String file = input.nextLine();
        readFile(file);
    }

    public static void readFile(String file) {
        try {
            Scanner sc = new Scanner(new File(file));
            dimension1 = sc.nextInt();
            dimension2 = sc.nextInt();
            array = new int[dimension1][dimension2];
            while (sc.hasNext()) {
                for (int row = 0; row < dimension1; row++) {
                    for (int column = 0; column < dimension2; column++) {
                        array[row][column] = sc.nextInt();
                        System.out.printf("%2d ", array[row][column]);
                    }
                    System.out.println();
                }

            }
            sc.close();
        }

        catch (Exception e) {
            System.out
            .println("Error: file not found or insufficient     requirements.");
        }
    }
}
like image 337
Sylvia Wang Avatar asked Oct 20 '22 09:10

Sylvia Wang


2 Answers

You're reading those numbers in this part of your code:

dimension1 = sc.nextInt();
dimension2 = sc.nextInt();

So dimension1 gets the value of 3 and dimension2gets the value of 5, but you're not saving them into the array.

like image 157
David Mendez Avatar answered Oct 22 '22 00:10

David Mendez


Try this code....

import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;

public class Proj4 {
    public static int rows, cols;
    public static int[][] cells;
    /**
     * main reads the file and starts
     * the graphical display
     */
    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        String file = JOptionPane.showInputDialog(null, "Enter the input file name: ");
        Scanner inFile = new Scanner(new File(file));

        rows = Integer.parseInt(inFile.nextLine());
        cols = Integer.parseInt(inFile.nextLine());
        cells = new int[rows][cols];

                //this is were I need help
        for(int i=0; i < rows; i++) 
        {
            String line = inFile.nextLine();
            line = line.substring(0);

        }

        inFile.close();

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(cells[i][j]);
            }
            System.out.print();
    }
like image 29
Jaffer Wilson Avatar answered Oct 21 '22 23:10

Jaffer Wilson