I have made some progress on a program that reads a .txt file:
soccer
Lions Panthers Tigers Sky
Panthers 4 Tigers 3
Tigers 3 Lions 1
Sky 2 Panthers 0
Lions 2 Tigers 1
Tigers 1 Sky 6
Sky 2 Panthers 1
Tigers 1 Sky 4
Panthers 3 Tigers 6
Lions 3 Sky 2
Tigers 4 Sky 3
Sky 2 Panthers 1
Lions 4 Panthers 6
Then out puts the win/loss records of each team in a new file "results.txt":
results
Team Wins Losses
Lions 2 2
Panthers 2 4
Sky 5 2
Tigers 3 4
So far my code does not properly keep track of each teams and losses. I began writing what will certainly be an extensive chain of if-else statements, but am not getting any results. There must be a better way of doing this that i am missing. here is what i have so far:
import java.io.*;
import java.util.*;
public class Soccer
{
public static void main (String[] args) throws IOException
{
//initiate an input file object and scanner object
File inData = new File("soccer.txt");
if(!inData.exists())
{
System.out.println("File does not exist");
System.exit(0);
}
Scanner input = new Scanner(inData);
//create output file and a printWriter object
File outData = new File("results.txt");
PrintWriter output = new PrintWriter(outData);
String teamName1 = input.next();
int wins1 = 0;
int loses1 = 0;
String teamName2 = input.next();
int wins2 = 0;
int loses2 = 0;
String teamName3 = input.next();
int wins3 = 0;
int loses3 = 0;
String teamName4 = input.next();
int wins4 = 0;
int loses4 = 0;
while(input.hasNext())
{
String teamOne = input.next();
int scoreOne = input.nextInt();
String teamTwo = input.next();
int scoreTwo = input.nextInt();
if(scoreOne>scoreTwo && teamOne==teamName1)
{
wins1++;
}
else if(scoreOne<scoreTwo && teamOne==teamName1)
{
loses1++;
}
}
output.println("Team\t\tWins\t\tLosses");
output.println(teamName1+"\t\t"+wins1+"\t\t"+loses1);
output.println(teamName2+"\t"+wins2+"\t\t"+loses2);
output.println(teamName3+"\t\t"+wins3+"\t\t"+loses3);
output.println(teamName4+"\t\t"+wins4+"\t\t"+loses4);
input.close();
output.close();
}
}