Reputation: 45
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.
Upvotes: 0
Views: 151
Reputation: 373
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.
Upvotes: 1
Reputation: 61
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
Upvotes: 0
Reputation: 195
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);
}
Upvotes: 1
Reputation: 1
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.
Upvotes: 0