Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用缓冲读取器将.txt文件转换为ArrayList,以及如何操作该对象?_Java_Arrays_Text_Arraylist_Bufferedreader - Fatal编程技术网

Java 如何使用缓冲读取器将.txt文件转换为ArrayList,以及如何操作该对象?

Java 如何使用缓冲读取器将.txt文件转换为ArrayList,以及如何操作该对象?,java,arrays,text,arraylist,bufferedreader,Java,Arrays,Text,Arraylist,Bufferedreader,我的项目首先从文本文件导入数据并将其转换为ArrayList。我对ArrayList不太熟悉,也不太知道它是如何工作的。我已经提供了讲师提供的初学者文件,看起来我需要使用缓冲读取器来导入文本文件。因为这门课程有一个习惯,就是在没有很好地解释代码的情况下提供初学者代码,我也希望能简单地解释一下bufferedreader是什么,如果我也有,我将如何自己创建一个。 以下是打开时显示的文本文件: manny 89 78 100 91 67 cindy 66 81 94 80 71 timmy 85

我的项目首先从文本文件导入数据并将其转换为ArrayList。我对ArrayList不太熟悉,也不太知道它是如何工作的。我已经提供了讲师提供的初学者文件,看起来我需要使用缓冲读取器来导入文本文件。因为这门课程有一个习惯,就是在没有很好地解释代码的情况下提供初学者代码,我也希望能简单地解释一下bufferedreader是什么,如果我也有,我将如何自己创建一个。 以下是打开时显示的文本文件:

manny 89 78 100 91 67
cindy 66 81 94  80 71
timmy 85 59 100 83 21
mikey 88 76 69  82 90
kelly 68 93 63  92 55
sandy 80 73 67  51 99
以下是启动程序代码:

    import java.util.*;
import java.io.*;

public class ExamScores
{
    public static void main( String args[] ) throws Exception
    {
            // close/reuse this file handle on the next file
            BufferedReader infile = new BufferedReader( new FileReader( "ExamScores.txt" ) );

            // you declare all needed ArrayLists and other variables from here on.

            System.out.println("\nSTEP #1: 50%");  // 50%

            System.out.println("\nSTEP #2: 25%");  // 75 %

            System.out.println("\nSTEP #3: 10%");  // 85%

            System.out.println("\nSTEP #4: 15%"); // 100%

    } // END MAIN

    // - - - - - - H E L P E R   M E T H O D S   H E R E - - - - -


} // END EXAMSCORES CLASS
一旦我创建了一个ArrayList,我该如何操作它来做一些事情,比如按照字母顺序对名称进行排序。ArrayList很像二维数组,还是文本文件中的每一行都是一个单独的数组列表?为了对我在这个项目中需要做的每件事都有一个想法,我必须按照数组列表中的字母顺序对名称进行排序。然后我需要计算每个名字的平均分数,最后我需要对每个学生进行第一、三和五次考试的平均值,找出他们在这三个考试中,哪一个是他们的平均分最低的?我不是在讨好这个项目的确切答案,但我想提供所有信息,说明我在这一点上可以使用arraylist做什么。谢谢。

您可以用字符串填充ArrayList

    BufferedReader inFile = new BufferedReader(new FileReader("ExamScores.txt"));

    //Define and initialise the ArrayList
    ArrayList<String> examScores = new ArrayList<String>(); //The ArrayList stores strings

    String inLine; //Buffer to store the current line
    while ((inLine = inFile.readLine()) != null) //Read line-by-line, until end of file
    {
        examScores.add(inline);
    }
    inFile.close(); //We've finished reading the file        

排序ArrayList比较困难。对于您的程序,最好解析字符串并将值存储到自定义StudentExams类中。这里有一个我为您制作的原型示例

    public class StudentExams
    {
        private String studentName;
        private int examScores[];

        //Inline is the String we get from inFile.readLine();
        public StudentExams(String inline)
        {
            //Pesudo-code
            parse the string using String.split(" ");
            store parsed values into studentName and examScores;
        }

        public String getStudentName()
        {
            return studentName;
        }

        public int[] getExamScores()
        {
            return examScores;
        }
    }
文件

教程:



我没有给你一个直接的答案,但我已经给了你足够的信息让你自己拼凑出答案。您可以阅读上述主题的所有教程。互联网上有大量的例子,它们完全可以做你想做的事情,你只需要尝试。

“本课程有一个习惯,即提供入门代码,但没有很好地解释代码的作用。”一个好的商人从不责怪工具。在这里,试图在做同样的事情的基础上获得帮助通常不会成功。Java是一种面向对象的语言。学习定义自己的类。您应该创建一个
类(或
记录
,或任何您想命名的类),其中包含文件行中包含的信息。读取该文件将创建一个
列表
。然后,您将在谷歌上搜索“如何在Java中对对象列表进行排序”。
    public class StudentExams
    {
        private String studentName;
        private int examScores[];

        //Inline is the String we get from inFile.readLine();
        public StudentExams(String inline)
        {
            //Pesudo-code
            parse the string using String.split(" ");
            store parsed values into studentName and examScores;
        }

        public String getStudentName()
        {
            return studentName;
        }

        public int[] getExamScores()
        {
            return examScores;
        }
    }