Read Percentage Marks from a Text File in Java

Write a program to read percentage marks of some students stored in the text file “marks.txt”. Now calculate the grade and display it on the screen according to the following conditions:

Percentage marks    Grade
>= 80 to <= 100             A 
>= 60 to < 80                 B 
>= 40 to < 60                 C
< 40                                  F
import java.io.*;
class Percentage{
    public static void main(String args[])throws IOException{
        FileReader fr = new FileReader("marks.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = new String();
        while((line = br.readLine()) != null){
            double p = Double.parseDouble(line);
            if(p >= 80 && p <= 100) 
                System.out.println(p + " - A"); 
            else if(p >= 60 && p < 80) 
                System.out.println(p + " - B"); 
            else if(p >= 40 && p < 60)
                System.out.println(p + " - C");
            else if(p < 40)
                System.out.println(p + " - F");
        }
    }
}

2 thoughts on “Read Percentage Marks from a Text File in Java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.