1

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
4
  • 1
    Check out this related answer about formatting strings. The main problem with simple printing is that the strings are of variable length. Commented Apr 20, 2019 at 15:16
  • You cannot arbitrarily assume all strings are the same length and that a tab character is sufficient to ensure things lining up in columns. If you want to ensure alignment you need to pad to specific lengths either manually or via built-in Java print formatting. Commented Apr 20, 2019 at 15:16
  • Days ago I answered very similarly to this one Commented Apr 20, 2019 at 15:27
  • @LakshanDissanayake he didn't write any code where as I have written some code. the questions are the same but I have the problem with output not the whole thing Commented Apr 20, 2019 at 15:35

1 Answer 1

0

You can use this formatting:

System.out.printf("   %-13s  %-10s%-15s%-5s","Home team","Score","Away team","Score\n"); 
System.out.println("===============   =====     =========      =====");
System.out.printf("%-17s   %-2s%15s %9s",hometeam,homescore,awayteam,awayscore);

Output:

Home team         Score     Away team       Score
=========         =====     =========       ======
Manchester United   2       Spurs             1

Do let me know if this is what you are looking for, if not I am here to edit it as per your requirement.

Sign up to request clarification or add additional context in comments.

1 Comment

Thisis exactly what i am looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.