对 ArrayList 元素执行字符串追加和算术运算<String>



我对java很陌生,所以请忽略我明显的错误。我有一个 txt 文件,其格式如下:

Student Name,Mathmatics_Marks,Physics_Marks,Chemistry_Marks,Biology_Marks
A,10,20,30,40
B,15,15,48,69
C,45,48,48,79
D,48,15,12,55

期望输出:

I need to do the following output format from the above txt files:
Student Name (Appended with "Student:" prefix)
Pass/Fail (Appended with "Exam Status:" prefix)
Average_Marks (Appended with "Average_Marks:" prefix)
Maximum_Marks(Appended with "Maximum_Marks:" prefix)
Minimum_Marks(Appended with "Minimum_Marks:" prefix)
For example:
Student: A
Exam Status: Pass
Average_Marks:25
Maximum_Marks:40
Minimum_Marks:10
<<so on for other students>>
...........................
...........................
...........................
...........................

用于所需输出的逻辑/算法:

1) If (Mathmatics_Marks+Physics_Marks+Chemistry_Marks,Biology_Marks)=>100 then Pass else Fail
2) Find out the average marks of student.
3) Write Maximum marks
4) Write Minimum marks

我的方法 :1-我能够使用以下代码将数据加载到ArrayList并从txt文件打印数据,但无法实现所需的输出

public static void main(String[] args)
    {
        //Input file path
        String fileToParse = "C:\Users\DELL-PC\Desktop\Analysis.txt";
        BufferedReader fileReader = null;
        //Delimiter Declaration
        final String DELIMITER = ",";
        try
        {
            List<String> Student_log= new ArrayList<String>();
            String line = "";
            //Create the file reader
            fileReader = new BufferedReader(new FileReader(fileToParse));
            //Reading the file line by line
            while ((line = fileReader.readLine()) != null) 
            {
                //Get all tokens available in line
                String[] tokens = line.split(DELIMITER);
                for(String token : tokens)
                {
                    //Print all tokens
                    /// Array Initialization Part 
                    System.out.println(token);
                    Student_log.add(token);
                }
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally
        {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

请帮助我编写代码。

public static void main(String[] args)
{
    //Input file path
    String fileToParse = "C:\Users\DELL-PC\Desktop\Analysis.txt";
    BufferedReader fileReader = null;
    //Delimiter Declaration
    final String DELIMITER = ",";
    try
    {
        List<String> Student_log= new ArrayList<String>();
        String line = "";
        //Create the file reader
        fileReader = new BufferedReader(new FileReader(fileToParse));
        //Reading the file line by line
        while ((line = fileReader.readLine()) != null) 
        {
            //Get all tokens available in line
            String[] tokens = line.split(DELIMITER);
            student_log = new ArrayList<>();
            for(String token : tokens)
            {
                //Print all tokens
                /// Array Initialization Part 
                Student_log.add(token);
            }
            // calculate average
            int sum = 0;
            for(int i=1; i<student_log.size();i++){            
            sum = sum + Integer.parseInt(student_log.get(i));            
            }
            double average = sum/(student_log.size()-1);
            String result = "fail";
            if(average > 100){
            result = "pass";
            }
            int max = Integer.parseInt(student_log.get(1));
            //Calculate max
            for(int i=1; i<student_log.size(); i++){
            if(max < Integer.parseInt(student_log.get(i)))
            max = Integer.parseInt(student_log.get(i));
            }
            //Calculate min
            int min = Integer.parseInt(student_log.get(1));
            //Calculate max
            for(int i=1; i<student_log.size(); i++){
            if(min > Integer.parseInt(student_log.get(i)))
            min = Integer.parseInt(student_log.get(i));
            }
            System.out.println("Student: " + student_log.get(0));
            System.out.println("Exam_status: " + result);
            System.out.println("Average marks: " +average);
            System.out.println("Maximum marks: " +max);
            System.out.println("Minimum marks: " +min);
            }
            } 
            catch (Exception e) {
         e.printStackTrace();
    } 
    finally
    {
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这将正常工作,但实际上,您将所有内容都存储到字符串数组列表中。我建议您创建一个类的模型,名称为字符串,所有其他标记为整数。这将是一个很好的编程实践,但这是一个简单的程序,在这种情况下你可以使用 Integer.parseInt()。

但是,我想在这里提出一点。计算最大值和最小值时,切勿为初始最大值和最小值变量分配 0。通常许多人这样做。因为,在某些情况下会有负面影响(不是在这种情况下)。但是,在这种情况下,如果我们将 min 指定为 0,则永远不会进入循环。

您需要初始化一些变量来计算平均值、最小值、最大值等。然后在 while 循环中从每行检索值并进行计算。

Example Code:
     while ((line = fileReader.readLine()) != null)  {
                    //Get all tokens available in line
                    String[] tokens = line.split(DELIMITER);
                    name = tokens[0];
                    math = Integer.valueOf(tokens[1]);
                    physics =Integer.valueOf(tokens[2]);
                    chem = Integer.valueOf(tokens[3]);
                    bio = Integer.valueOf(tokens[4]);
                    if (math + physics + chem + bio > 100) {
                        pass = "Pass"; 
                    } else {
                        pass = "Fail";
                    }
                    System.out.println("Student: "+ name);
                    System.out.println("Exam Status: "+ pass);
                } 

你可能有一些问题。首先,您需要一些计算来计算通过/失败,最大值,最小值等。因此,您需要先遍历值以确定,转换为整数,依此类推。然后,计算值是您要打印出的值,但手动使用一些附加文本,而不是在该循环中。所以一些Student_log.add(文本);语句,每个计算一个。同时跳过文本的第一行。

您需要忽略第一行并为 4 个选项添加方法,例如

   getAverageMark(List marks) {
     //find average
   }

其中 列表标记 - 特定学生的所有标记,您必须阅读每一行

相关内容

最新更新