从.text文件中读取输入并将结果放入2D数组中

时间:2014-06-11 06:52:15

标签: java io java.util.scanner

此程序读取float值,但通过四舍五入文本文件中的输入值将其显示为最多8位小数。有人能告诉我如何读取值并打印完全相同的值?一个简单的Java程序(提示 - 可以作为字符串读取)我是Java的初学者,所以不知道很多东西。我不知道如何实现它。

import java.util.Scanner;
import java.io.File;

public class read {
    public static double[][] readData(String filename) throws Exception {
        double[][] matrix = {{1}, {2}};
        File inFile = new File(filename);
        Scanner in = new Scanner(inFile);
        int intLength = 0;
        String[] length = in.nextLine().trim().split("\\s+");
        for (int i = 0; i < length.length; i++) {
            intLength++;
        }

        in.close();

        matrix = new double[intLength][intLength];

        in = new Scanner(inFile);
        int lineCount = 0;
        while (in.hasNextLine()) {
            String[] currentLine = in.nextLine().trim().split("\\s+"); 
            for (int i = 0; i < currentLine.length; i++) {
                matrix[lineCount][i] = Float.parseFloat(currentLine[i]);    
            }

            lineCount++;
        }

        return matrix;
    }

    public static boolean isMagicSquare(int[][] square) {
        return false;
    }

    public static void main(String args[])
    {
        double[][] mat;
        try {
            mat = readData("readsample.txt");
            int ii = 347;
            int jj = 697;
            for(int i = 0; i < ii; i++) {
                for(int j = 0; j < jj; j++) {
                    System.out.print(mat[0][0] + " ");
                }

                System.out.println(" ");
            }

            System.out.print(mat[i][j] + " ");

            double m1 = mat[0][0], m2 = mat[0][0];  
            for(int i = 0; i < ii; i++) {
                for(int j = 0; j < jj; j++) {
                    if(mat[i][j] < m1) {
                        m1 = mat[i][j]; 
                    }
                    //if(m2 > mat[i][j])
                    //    m2 = mat[i][j];
                }
            }

            System.out.print(m1 + " ");
        }
        catch(Exception e) { }
    }
}

0 个答案:

没有答案