0% found this document useful (0 votes)
7 views2 pages

Import Java 1122

The Java program reads student names and scores from a string, calculates the average score while handling invalid scores through a custom exception. It prints valid scores and reports errors for scores outside the 0-100 range. The final output includes the average score of valid entries.

Uploaded by

tushu1110
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Import Java 1122

The Java program reads student names and scores from a string, calculates the average score while handling invalid scores through a custom exception. It prints valid scores and reports errors for scores outside the 0-100 range. The final output includes the average score of valid entries.

Uploaded by

tushu1110
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

BufferedReader;
import [Link];
import [Link];
// Custom Exception
class InvalidScoreException extends Exception {
InvalidScoreException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args)
String data =
"Aman 78\n" +
"Riya 92\n" +
"Kabir 45\n" +
"Anita -5\n" +
"Rahul 110";

double total = 0;
int count = 0;
try {
BufferedReader br = new BufferedReader(new StringReader(data));
String line;
while ((line = [Link]()) != null) {
String[] parts = [Link](" ");
String name = parts[0];
int score = [Link](parts[1]);
try {
if (score < 0 || score > 100) {
throw new InvalidScoreException("Invalid score for " + name);
}
[Link](name + " : " + score);
total += score;
count++;
} catch (InvalidScoreException e) {
[Link]([Link]());
}
}
double avg = total / count;
[Link]("Average Score = %.2f\n", avg);
} catch (IOException e) {
[Link]("Error reading data");
}
}
}
OUTPUT: Aman : 78
Riya : 92
Kabir : 45
Invalid score for Anita
Invalid score for Rahul
Average Score = 71.67

You might also like