繁体   English   中英

从 txt 文件 java 打印二维数组

[英]Printing a 2d array from a txt file java

txt file:

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

我有一个 txt 文件,它是整数和双精度网格,我应该将其转换为没有 arraylist 的二维数组。 我找到了文件的行和列,并将它们设置为 2d 双精度数组的大小,但是当我运行它时它不起作用,它正在跳过打印数组部分并转而去捕获。

更新:我看到我在下一个双倍中使用了错误的扫描仪,我现在将其更改为第二个扫描仪,它现在正在打印阵列,而不是第一行,为什么?

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

问题是,当您使用 scan.nextDouble() 将值放入数组时,扫描 object 在经过第一个循环后已经到达文件末尾。 因此,您必须创建扫描仪 class 的新扫描 object。

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(); 
    }
}

output 是:

 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

问题出在这一行

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

我们应该使用扫描仪而不是扫描仪

代码:

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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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