The program should read the match data from a text file. Each line in the file contains the data for a specific match. The format of each line within the file is as follows:
home_team_name : away_team_name : home_team_score : away_team_score
Notice how a colon character (:) is used to separate different parts of the input (the field delimiter).
The following is an example of the typical lines of data within the file.
Arsenal : Spurs : 2 : 1
Everton : Liverpool : 1 : 1
Huddersfield : Chelsea: 2 : 1
The program should prompt the user to enter the name of match data file, then it should read, store, and process each line of match data stored in the file and output the data to the console in the specified format (i.e., this is not just reading and displaying the raw data as stored in the file).
The match data must be displayed in the following format.
Home team Score Away team Score
========= ===== ========= ======
Arsenal 2 Spurs 1
Everton 1 Liverpool 1
Huddersfield 2 Chelsea 1
I Have done the code but the output doesn't come as it shows in the question till this:
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class FootallResultsGenerator {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String[] splitarray=new String[4];
String filename;
String line;
String hometeam;
String homescore;
String awayteam;
String awayscore;
Scanner obj=new Scanner(System.in);
System.out.println("Enter the name of the file");
filename=obj.nextLine();
Scanner filereader=null;
try {
File fileobject=new File(filename);
filereader=new Scanner(fileobject);
System.out.println("Home team\t"+"Score\t"+"Away team\t"+"Score");
System.out.println("==========\t"+"======\t"+"=========\t"+"======");
while(filereader.hasNext())
{
line=filereader.nextLine();
splitarray=line.split(":");
if(splitarray.length==4) {
hometeam=splitarray[0];
homescore=splitarray[2];
awayteam=splitarray[1];
awayscore=splitarray[3];
System.out.println(hometeam+"\t"+homescore+"\t"+awayteam+"\t"+awayscore);
}
}
}
catch(FileNotFoundException e)
{
System.out.println("Error:File Not Found.");
}
}
}
It should display:
Home team Score Away team Score
========= ===== ========= ======
Manchester United 2 Spurs 1
Everton 1 Liverpool 1
Huddersfield 2 Chelsea 1
Instead is displaying this:
Home team Score Away team Score
========= ===== ========= ======
Manchester United 2 Spurs 1
Everton 1 Everton 1
Huddersfield 2 Chelsea 1