In: Computer Science
Suppose a file (whose name is given through an argument by the user) contains student names and their scores (ranging from 0 to 100): i.e. each line contains a student name (just the first name with no space inside) and the student’s score separated by a space. Find out who has the highest score with what score, and who has the lowest score with what score. Also, calculate the average of all of the scores. Do not use arrays to solve this problem. (java)
// scores.txt (input file)
James 87
Mary 65
Billy 98
Robinson 67
Kane 65
Williams 95
/******************************************************/
/******** ReadFileScores.java ********/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class ReadFileScores {
public static void main(String[] args)
{
String
maxName="",minName="",name1,name2,line;
int
min=0,max=0,score1=0,score2=0;
Scanner sc=null;
String filename;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner input = new
Scanner(System.in);
System.out.print("Enter filename
:");
filename=input.nextLine();
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
try {
sc = new
Scanner(new File(filename));
line=sc.nextLine();
String
arr[]=line.split(" ");
name1=arr[0];
score1=Integer.parseInt(arr[1]);
max=score1;
min=score1;
while(sc.hasNext())
{
line=sc.nextLine();
arr=line.split(" ");
name2=arr[0];
score2=Integer.parseInt(arr[1]);
if(max<score2)
{
max=score2;
maxName=name2;
}
if(min>score2)
{
min=score2;
minName=name2;
}
}
} catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
sc.close();
System.out.println("The Student who
got minimum score "+min+" is :"+minName);
System.out.println("The Student who
got maximum score "+max+" is :"+maxName);
}
}
/******************************************************/
/******************************************************/
Output:
/******************************************************/