簡體   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