package exercises; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * * @author Lefteris Moussiades */ public class FindBest { public static void read() throws IOException { BufferedReader inputStream = new BufferedReader(new FileReader("studentInfo.txt")); String l; int[] spacePos = new int[6]; double max = -1; String winnerFName = null, winnerSName = null; while ((l = inputStream.readLine()) != null) { for (int i = 0; i < 6; i++) { if (i == 0) { spacePos[0] = l.indexOf(' '); } else { spacePos[i] = l.indexOf(' ', spacePos[i - 1] + 1); } if (spacePos[i] == -1) { throw new RuntimeException(); } } String fName = l.substring(0, spacePos[0]); String sName = l.substring(spacePos[0] + 1, spacePos[1]); int sum = 0; for (int i = 1; i < 6; i++) { if (i < 5) { sum += Integer.parseInt(l.substring(spacePos[i] + 1, spacePos[i + 1])); } else { sum += Integer.parseInt(l.substring(spacePos[i] + 1)); } } double average = sum / 5.; if (average > max) { max = average; winnerFName = fName; winnerSName = sName; } } System.out.println(winnerFName + " " + winnerSName + " " + max); inputStream.close(); } static void write() throws IOException { BufferedWriter f=new BufferedWriter(new FileWriter("studentBackUp.txt")); f.append("John Black"); f.newLine(); f.append("Mary Green"); f.close(); } public static void main(String[] args) throws IOException { //read(); write(); } }