Exercise - 4: Working with files In this exercise, you will implement the marath
ID: 3875590 • Letter: E
Question
Exercise - 4: Working with files
In this exercise, you will implement the marathon exercise using input and output files. Assume that the names and times are given in a file. A sample file is given in the atachment: “marathon.txt”. Read the file and fill out the names and times array. Write method to find the best and second best runners. You are supposed to write the names and times of the best and second best runners in an output text file, called: “marathon-result.txt”.
Name Time(minutes)
Elena 341
Thomas 273
Hamilton 278
Suzie 329
Phil 445
Matt 402
Alex 388
Emma 275
John 243
James 334
Jane 412
Emily 393
Explanation / Answer
To impliment this logic, I am using Java technology. I have tried to explain the code through comments.
To run this method please create two files on this location: D:Chegg
Add following details of runners in marathon.txt :
Name Time(minutes)
Elena 341
Thomas 273
Hamilton 278
Suzie 329
Phil 445
Matt 402
Alex 388
Emma 275
John 243
James 334
Jane 412
Emily 393
BestRunners.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class BestRunners {
public static void main(String[] args) {
File file = new File("D:\Chegg\marathon.txt"); // searching for a file in a given path
try { // TRY and Catch to check file is exist or not
Scanner sc = new Scanner(file);
List<Integer> lstTime = new ArrayList<>(); // Initialize the list of times
HashMap<Integer, String> hm = new HashMap<Integer, String>();
if (sc.hasNextLine()) { // check for data is exist or not in file
sc.nextLine(); // avoiding headers of file (Name and Time)
String name;
Integer time = null;
while (sc.hasNextLine()) { // Loop to check next line of data
String line = String.valueOf(sc.nextLine()); //Reading lines of text file
String[] splited = line.split("\s+"); //Splitting line by space
name = "";
for (int i = 0; i < splited.length; i++) { //Differentiating name and time
if (i != splited.length - 1) {
name += splited[i];
} else {
time = Integer.valueOf(splited[i]);
}
}
lstTime.add(time); //Adding time column value in list
hm.put(time, name); //Putting time and name in hashmap
}
Collections.sort(lstTime); //Sorting time to get first and second best runner
List<String> lstWinners = new ArrayList<>(); //List of best runner
for (int i = 0; i < lstTime.size(); i++) {
lstWinners.add(hm.get(lstTime.get(i)));
}
PrintWriter writer;
try {
writer = new PrintWriter("D:\Chegg\marathon-result.txt","UTF-8"); // searching for a file in a given path
writer.println("Rank Name Time(Minutes) "); // Printing headers
for (int i = 0; i < 2; i++) { //Printing top two runners data in file
writer.println("(" + (i + 1) + ") " + lstWinners.get(i)
+ " " + lstTime.get(i) + " ");
}
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
System.out
.println("--------------- File has no data inside ---------------------");
}
} catch (FileNotFoundException e) {
System.out.println("Error: File does not exist " + e);
e.printStackTrace();
}
}
}
OUTPUT:
in output you will find the best two runners in marathon-result.txt file as showing below.
Rank Name Time(Minutes)
(1) John 243
(2) Thomas 273
If you have any query regarding this problem then please feel free to ask in comment.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.