【问题标题】:Printing a 2d array from a txt file java从 txt 文件 java 打印二维数组
【发布时间】:2022-01-29 01:46:04
【问题描述】:
txt file:

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

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

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

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

【问题讨论】:

  • 能否显示正在阅读的文件内容
  • 什么捕获块?你一个都不显示。什么例外?文件变量 null 是不是因为路径不正确导致抛出 NullPointerException?
  • 问题是这样的: }catch(Exception e){ System.out.println("错误发生..."); }
  • 但这不是问题,因为当我对行和列进行硬编码时它运行良好
  • 再说一次,有什么例外?您是否再次检查过文件路径是否正确?什么都不假设,证明一切

标签: java arrays


【解决方案1】:

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

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

输出是:

 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

【讨论】:

  • 知道了,谢谢!!
【解决方案2】:

问题出在这一行

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

输出:

$ 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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多