简体   繁体   中英

Printing a 2d array from a txt file java

txt file:

10.3 20 30 40
9 2 10 7 
3 8 4 1

I have a txt file it is a grid of integers and doubles I am supposed to convert it into a 2d array without arraylist. I found the rows and columns of the file and I set them as the size for the 2d double array but it is not working when I run it, it is skipping the printing array part and going to the catch instead.

UPDATE: I saw that I used the wrong scanner in the next double I have changed it to the second scanner now and it is printing the array now just not the first line why?

File file = new File("input.txt");
    
            System.out.print(arr[i][j] + " ");
           
        }
        System.out.println();
    }

The problem is that when you are putting the values in the array using scan.nextDouble(), that scan object has already reached the end of file after going through the first loop. Therefore you have to create a new scan object of Scanner class.

public class ReadingFromFile {
    
    public static void main(String args[]) throws FileNotFoundException {
        File file = new File("input.txt");
        Scanner scan = new Scanner(file); 
        int row = 0;
        int col = 0;
        while (scan.hasNextLine()){
            String line = scan.nextLine();
            row++;     
        }
        System.out.println("no of rows--------" +row);

         scan = new Scanner (file);
        String sn = scan.nextLine();
        String [] otLine = sn.split(" ");
        for (int i = 0; i < otLine.length; i++){
            col++;
        }
        System.out.println("no of columns -----"+col);
        
         scan = new Scanner (file);
        double [][] arr = new double[row][col];   
        for(int i = 0; i < row; i++) {       
            for(int j = 0; j < col; j++) {
                arr[i][j] = scan.nextDouble();
                System.out.print("\t"+arr[i][j] );
               
            }
            System.out.println();
        }
        scan.close(); 
    }
}

and the output is:

 no of rows--------3
no of columns -----4
    10.3    20.0    30.0    40.0
    9.0     2.0     10.0     7.0
    3.0     8.0     4.0     1.0

Problem is in this line

arr[i][j] = scan.nextDouble();

instead of scan we should be using scanner

Code:

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

class MainClass {
    public static void main(String ... $) throws Exception{
        var out = System.out;
        File file = new File("input.txt");
        Scanner scan = new Scanner(file); 
        int row = 0;
        int col = 0;
        while (scan.hasNextLine()){
            String line = scan.nextLine();
            out.println(line);
            row++;     
            if (col==0)
                col=line.split(" ").length;
        }
        //close it
        scan.close();
        out.println("rows : "+row);

        Scanner scanner = new Scanner (file);
        //String sn = scanner.nextLine();
        //String [] otLine = sn.split(" ");
        //for (int i = 0; i < otLine.length; i++){
        //  col++;
        //}
        System.out.println("cols : "+col);
        //arr will store out 2d array
        double [][] arr = new double[row][col];   
        for(int i = 0; i < row; i++) {       
            for(int j = 0; j < col; j++) {
                arr[i][j]=scanner.nextDouble();
                //System.out.print(arr[i][j] + " ");

            }
            //System.out.println();
        }
        //displaying the array
        for(int i = 0; i < row ; i++){
            for(int j=0;j<col;j++){
                out.print(arr[i][j]+" ");
            }
            out.println();
        }
        scanner.close();
    }
}

output:

$ javac MainClass.java && java MainClass 
10.3 20 30 40
9 2 10 7 
3 8 4 1
rows : 3
cols : 4
10.3 20.0 30.0 40.0 
9.0 2.0 10.0 7.0 
3.0 8.0 4.0 1.0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM