Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

how to find home team name may be misssing ? in java project football result generator

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 the 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 is 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      |
------------------------------------------------------

Requirement 3 (20 marks)

The data stored in the file could be corrupted. For this requirement, the program must examine each line of data read from the file, identify invalid data and report these to the user via messages on the console. ?At a minimum, the program should check and validate the following possible issues

The home team name may be missing.
The away team name may be missing.
The home team score may be missing.
The away team score may be missing.
The field delimiter may be missing or wrong field delimiter is used.
Home team score may not be a valid integer number.
Away team score may not be a valid integer number.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can read the file using FileReader and read it line by line.

Then split each line with your delimiter, which is in this case is :

If there are 4 elements then that line is properly formatted.

Iterate through all element and check whether is it blank.

and finally, try parsing 2nd and 4th element which validates scores.

Here is a pseudo code

//read file
String[] lines = readFile();

//iterate through records
for String line: lines {
    String[] elems = line.split(":");
    if elems.size() < 4 {
        //invalid delimeter used
        continue;
    }

    // Do the validation below
    if String.isEmpty(elems[0]) {
        //home team missing
    }

    // likewise check for empty elements

    // validating numbers
    try{
        Integer.parse(elems[1]);
    } catch (NumberFormatException ex) {
        // invalid home score
    }

    //like wise check for invalid scores
}

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...