Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading 2-D array from a file

Tags:

java

I have a 2-D int array in file 'array.txt'. I am trying to read all the elements in the file in a two dimensional array. I am having problem in copying. It shows all the elements having value '0' after copying instead their original value. Please help me. My code is :

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

public class appMainNineSix {

    /**
     * @param args
     */
    public static void main(String[] args) 
        throws java.io.FileNotFoundException{
        // TODO Auto-generated method stub
        Scanner input = new Scanner (new File("src/array.txt"));
        int m = 3;
        int n = 5;
        int[][] a = new int [m][n];
        while (input.next()!=null){
            for (int i=0;i<m;i++){
                for (int j=0;j<n;j++)
                    a[i][j]= input.nextInt();
            }   

        }
        //print the input matrix
        System.out.println("The input sorted matrix is : ");
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++)
                System.out.println(a[i][j]);
        }

    }

}
like image 839
Neel Avatar asked Jan 21 '23 16:01

Neel


2 Answers

while (input.next()!=null)

This will consume something from the scanner input stream. Instead, try using while (input.hasNextInt())

Depending on how robust you want your code to be, you should also check inside the for loop that something is available to be read.

Scanner input = new Scanner (new File("src/array.txt"));
// pre-read in the number of rows/columns
int rows = 0;
int columns = 0;
while(input.hasNextLine())
{
    ++rows;
    Scanner colReader = new Scanner(input.nextLine());
    while(colReader.hasNextInt())
    {
        ++columns;
    }
}
int[][] a = new int[rows][columns];

input.close();

// read in the data
input = new Scanner(new File("src/array.txt"));
for(int i = 0; i &lt rows; ++i)
{
    for(int j = 0; j &lt columns; ++j)
    {
        if(input.hasNextInt())
        {
            a[i][j] = input.nextInt();
        }
    }
}

An alternative using ArrayLists (no pre-reading required):

// read in the data
ArrayList&ltArrayList&ltInteger&gt&gt a = new ArrayList&ltArrayList&ltInteger&gt&gt();
Scanner input = new Scanner(new File("src/array.txt"));
while(input.hasNextLine())
{
    Scanner colReader = new Scanner(input.nextLine());
    ArrayList col = new ArrayList();
    while(colReader.hasNextInt())
    {
        col.add(colReader.nextInt());
    }
    a.add(col);
}
like image 129
helloworld922 Avatar answered Jan 24 '23 06:01

helloworld922


The problem is when u reach the end of the file it throughs an exception that no usch element exist.

 public static void main(String[] args) {
    // TODO Auto-generated method stub         
    try {
        Scanner input = new Scanner(new File("array.txt"));
        int m = 3;
        int n = 5;
        int[][] a = new int[m][n];
        while (input.hasNextLine()) {
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                   try{//    System.out.println("number is ");
                    a[i][j] = input.nextInt();
                      System.out.println("number is "+ a[i][j]);
                    }
                   catch (java.util.NoSuchElementException e) {
                       // e.printStackTrace();
                    }
                }
            }         //print the input matrix
            System.out.println("The input sorted matrix is : ");
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    System.out.println(a[i][j]);

                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I knew that making catch without processing the exception but it temporary works. Please be aware I put the file outside the source folder.

like image 28
palAlaa Avatar answered Jan 24 '23 05:01

palAlaa