0

I am very new to java so please ignore my obvious errors. I have an txt files whose format is as following:

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

Desired Output:

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>>
...........................
...........................
...........................
...........................

Logics/Algo for Desired Output:

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

My Approach : 1- I am able to load data to ArrayList and Print data from txt file using following code but unable to achieve the desired output:

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();
            }
        }
    }

Please help me in my code.

2
  • Where did you calculate avg, max, min and if the student has passed? Commented Feb 9, 2016 at 20:42
  • @NikithaReddy I am thinking and trying do it using an arraylist in built functions. Commented Feb 9, 2016 at 20:49

4 Answers 4

1
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();
        }
    }
}

This will work fine but as a matter of fact, you are storing everything into a array list of string. I would suggest you to create a model of the class, with name as string and all other marks as integers. That would be a good programming practice but then this is a simple program and you can use Integer.parseInt() in this case.

However, I would like to make a point here. When calculating max and min values never assign 0 to the initial max and min variables. Usually many do so. Because, there are cases when there would be negatives (not in this case). But, in this case if we assign min to be 0 then the loop is never entered.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your prompt answer I learnt a lot from your answer let me run this code
from your code second line onwards not working for any kind of correct set of inputs following errors is coming java.lang.NumberFormatException: For input string: "Student2" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at FProcessing.main(FProcessing.java:39)
Made changes :) sorry about that.. failed to re-initialize the array
1

You need to initialize some variables for calculating your average, min, max etc. And then inside your while loop retrieve values from each line and calculate.

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);

                } 

Comments

0

You may have a few problems. The first is that you need some calculations to work out the pass/fail, maximum, minimum values, etc. So you need to loop through the values first to determine that, cast to integer, and so on. Then the calculated values are what you want to print out, but manually with some additional text, not in that loop. So a few Student_log.add(text); statements, one for each calculation. Also skip the first line of text.

2 Comments

this answer is a bit hard to understand. some sample code, formatting, etc would be useful.
Yes, that listing by Nikitha Reddy looks OK. It was meant to be corrections to the algorithm.
0

You need ignore first line and add methods for your 4 options, e.g.

   getAverageMark(List marks) {
     //find average
   }

where List marks - all marks of a particular student, that you have to get a reading of each line

2 Comments

could you please give little more explanation likewise example explanation of your code.
Put the explanation in the answer and not in the comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.