简体   繁体   English

按列读取文本文件并在Java中创建2d数组

[英]Read Text File by column and create 2d array in java

How do I read the below text file by column, and find the min value for the grade array by column? 如何按列阅读​​以下文本文件,并按列查找成绩数组的最小值? Thanks in advance for your help. 在此先感谢您的帮助。

This is my text file 这是我的文字档

John  25 5 4.5 5 4 5 10 10 6 9.5 5.5
Jim   25 5 4 5 4.5 5 10 4 10 9.5 7.5
Kathy 15 1 3 2 1 1.5 8 2 4 3 4
Steve 21 5 3 2 1 4 5 6 7 8 8
Stacy 15 5 1 1 1 5 3 8 9 5 7
Faith 16 3 4 2 4 4 7 5 2 3 8

This is what I want the array to look like 这就是我想要的数组

Name = John, Jim, Kathy, Steve, Stacy, Faith
grade1 = 25 25 15 21 15 16
grade2 = 5 5 1 5 5 3
grade3 = 4.5 4 3 3 1 4

and so on .... 等等 ....

This is my code 这是我的代码

public static void main(String[] arg) throws IOException
{
    PrintWriter writer =  new PrintWriter(new FileOutputStream("Output.txt"));

    double[][] grades = null;

    findMinIndex(grades); //I call the function here just to test out my result before print out to text file.

     writer.println("Min: " + findMinIndex(grades));
     writer.close();
}

//This method to read the file -- have to add this part or the page wont' let me save my code -- //这种读取文件的方法-必须添加此部分,否则页面不会让我保存我的代码-

public static void processSection(PrintWriter writer, double[][]grades, String[] names) throws IOException
{
{
    Scanner in  = null;

    try
    {
        in = new Scanner(new FileInputStream("input.txt"));         

        //in.nextLine();

          int rows = in.nextInt();
          int columns = in.nextInt();             
          grades = new double[rows][columns];             
          names = new String[grades.length];    
          String[] col = null;

          while(in.hasNextLine()) 
          {     
              for (int i=0; i< grades.length; i++)
              {
                  col = in.nextLine().trim().split("\\s+");     

                  for (int j = 1; j < col.length; j++)                     
                  {                              
                      names[i] = col[0];

                      grades[i][j] = Double.parseDouble(col[j]);

                  }   
              }         
          }  

         in.close();     
    }

    catch(Exception e)
    {

    }

}

//This method to find Min
public static double findMinIndex(double[][] grades) throws FileNotFoundException, IOException 
{       
        double min = grades[0][0];  

        for (int j = 0; j < grades.length; j++) 
        {
            min = Integer.MAX_VALUE;

            for (int i = 1; i < grades[j].length; i++) 
            {                                       
                if (grades[j][i] < min) 
                {                   
                    min = grades[j][i];                      
                }
            }               
        }

        System.out.println(min);
        return min;
}

//The result I got is to calculate the min by the row not by the column.
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.Arrays;

class Create2DArray {

    public static void main(String[] arg) throws IOException {
        String[] names = getNames();
        List namesArrayList = Arrays.asList(names);
        String stringNames = namesArrayList.toString()
                .replace("[", "")
                .replace("]", "")
                .trim();         

        double[][] grades = getGrades();

        System.out.println("Name = " + stringNames);

        double rowsum = 0;
        for (int i = 0; i < grades[0].length; i++) {
            System.out.print("grade" + (i+1) + " = ");
            for (int j = 0; j < grades.length; j++) {
                System.out.print(grades[j][i] + " ");
                rowsum += grades[j][i];
            }
            System.out.println("; Row sum = " + rowsum);
            rowsum = 0;
        }
    }

    private static String[] getNames() throws IOException {
        String[] names = null;

        BufferedReader buffer = new BufferedReader(new FileReader("input.txt"));

        String line;
        int row = 0;
        int size = 0;

        BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
        int lines = 0;
        while (reader.readLine() != null) lines++;
        reader.close();

        while ((line = buffer.readLine()) != null) {
            String name = line.trim().split("\\s+")[0];

            if (names == null) {
                size = lines;
                names = new String[size];
            }

            names[row] = name;
            row++;
        }
        return names;
    }

    private static double[][] getGrades() throws IOException {
        double[][] matrix = null;

        BufferedReader buffer = new BufferedReader(new FileReader("input.txt"));

        String line;
        int nrows = 0;
        int row = 0;
        int ncols = 0;

        while (buffer.readLine() != null) nrows++;
        buffer.close();

        BufferedReader reader = new BufferedReader(new FileReader("input.txt"));

        while ((line = reader.readLine()) != null) {
            String[] vals = line.trim().split("\\s+");
            ncols = vals.length-1;

            if (matrix == null) {
                matrix = new double[nrows][ncols];
            }

            for (int col = 1; col <= ncols; col++) {
                try {
                    matrix[row][col-1] = Double.parseDouble(vals[col]);
                }
                catch (NumberFormatException e) {}
            }
            row++;
        }
        reader.close();
        return matrix;
    }
}

output: 输出:

Name = John, Jim, Kathy, Steve, Stacy, Faith
grade1 = 25.0 25.0 15.0 21.0 15.0 16.0 ; Row sum = 117.0
grade2 = 5.0 5.0 1.0 5.0 5.0 3.0 ; Row sum = 24.0
grade3 = 4.5 4.0 3.0 3.0 1.0 4.0 ; Row sum = 19.5
grade4 = 5.0 5.0 2.0 2.0 1.0 2.0 ; Row sum = 17.0
grade5 = 4.0 4.5 1.0 1.0 1.0 4.0 ; Row sum = 15.5
grade6 = 5.0 5.0 1.5 4.0 5.0 4.0 ; Row sum = 24.5
grade7 = 10.0 10.0 8.0 5.0 3.0 7.0 ; Row sum = 43.0
grade8 = 10.0 4.0 2.0 6.0 8.0 5.0 ; Row sum = 35.0
grade9 = 6.0 10.0 4.0 7.0 9.0 2.0 ; Row sum = 38.0
grade10 = 9.5 9.5 3.0 8.0 5.0 3.0 ; Row sum = 38.0
grade11 = 5.5 7.5 4.0 8.0 7.0 8.0 ; Row sum = 40.0

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

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