Skip to content

Instantly share code, notes, and snippets.

@kurtkaiser
Last active August 14, 2018 19:29
Show Gist options
  • Save kurtkaiser/2acc911e1aa9616ecf4d06dec58cc9d5 to your computer and use it in GitHub Desktop.
Save kurtkaiser/2acc911e1aa9616ecf4d06dec58cc9d5 to your computer and use it in GitHub Desktop.
This was an assignment for an advanced java course at my local community college. It creates an array from a text file that contains data about the amount of time it takes a runner to complete a half-marathon. I was then required to create a way for a user to input a runner's number and get data about their race performance printed to the console.
/*
Kurt Kaiser
CTIM 168
07.28.2018
Homework: C10PP10
RFID Race Data Text File Log
*/
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DemoRaceDataLog {
// Demonstrating the program
public static void main(String[] args) {
String fileName =
"raceTextData.txt";
int[] runnerTimes; // = new int[3];
String splits;
int place; // = 0;
String[] overallTimes; // = new String[2];
try {
RaceDataLog recent = new RaceDataLog(fileName);
// User input
System.out.print("Enter a racer's number: ");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// Calling class methods
runnerTimes = recent.getTimes(input);
place = recent.getPlace(input);
System.out.println("Place: " + place);
splits = recent.getSplits(runnerTimes, 0);
System.out.println("Split 1-2: " + splits);
splits = recent.getSplits(runnerTimes, 1);
System.out.println("Split 2-3: " + splits);
overallTimes = recent.getOverallTimes(runnerTimes);
System.out.println("Overall Time: " + overallTimes[0]);
System.out.println("Overall Pace: " + overallTimes[1]);
} catch (FileNotFoundException e) {
System.out.println("Cannot find file " + fileName);
}
}
}
/*
Kurt Kaiser
CTIM 168
07.28.2018
Homework: C10PP10
RFID Race Data Text File Log
*/
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
public class RaceDataLog {
private String[][] data = new String[9][];
public String[][] getData() {
return data;
}
// Constructor
public RaceDataLog(String fileName) throws FileNotFoundException {
Scanner inputStream = new Scanner(new File(fileName));
String startTime = inputStream.nextLine();
int count = 0;
String breakUp = "[, ]";
// Read the rest of the file line by line
while (inputStream.hasNextLine()) {
// Use split to create two dimensional array
data[count] = inputStream.nextLine().split(breakUp);
count++;
}
inputStream.close();
}
// Returns run time in seconds of requested number
public int[] getTimes(String input) {
int[] runnerTimes = new int[3];
String[][] data = getData();
int count = 0;
int intTime;
int place = 1;
// Runners Place
for (int j = 0; j < data.length; j++) {
if (data[j][0].equals("2")) {
if (data[j][1].equals(input)) {
}
place++;
}
}
for (int i = 0; i < data.length; i++) {
if (input.contentEquals(data[i][1])) {
intTime = 3600 * Integer.parseInt(data[i][2]);
intTime += 60 * Integer.parseInt(data[i][3]);
intTime += Integer.parseInt(data[i][4]);
runnerTimes[count] = intTime;
count++;
}
}
return runnerTimes;
}
// Returns place of runner
public int getPlace(String input) {
String[][] data = getData();
int place = 1;
// Runners Place
for (int j = 0; j < data.length; j++) {
if (data[j][0].equals("2")) {
if (data[j][1].equals(input)) {
return place;
}
place++;
}
}
return 0;
}
// Calculate runners pace between markers, output to console
public String getSplits(int[] runnerTimes, int startSplit) {
int dist = 1;
if (startSplit == 0) {
dist = 7;
} else if (startSplit == 1) {
dist = 6;
} else {
return "error";
}
int split = (runnerTimes[startSplit + 1] - runnerTimes[startSplit]) / dist;
String result = getFormattedTime(split);
return result;
}
public String getFormattedTime(int total){
int[] timeArray = new int[3];
String strTime;
String result = "";
timeArray[0] = total / 3600;
timeArray[1] = (total - (timeArray[0] * 3600)) / 60;
timeArray[2] = total - timeArray[0] * 3600 - timeArray[1] * 60;
for (int i = 0; i < 3; i++) {
strTime = String.valueOf(timeArray[i]);
if (strTime.length() == 1) strTime = "0" + strTime;
result += strTime;
if (i < 2) result += ":";
}
return result;
}
// Calculate overall race time
public String[] getOverallTimes(int[] runnerTimes) {
String[] overallTimes = new String[2];
int total = runnerTimes[2] - runnerTimes[0];
for (int i = 0; i<2; i++) {
overallTimes[i] = getFormattedTime(total);
total = total / 13;
}
return overallTimes;
}
}
08 00 00
0,100,08 00 00
0,132,08 00 03
0,182,08 00 15
1,100,08 50 46
1,182,08 51 15
1,132,08 51 18
2,132,09 34 16
2,100,09 35 10
2,182,09 45 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment