从文本文件过滤特定团队并显示结果

时间:2018-12-06 17:40:43

标签: java io formatting

我希望程序允许用户输入团队名称,并基于该名称将相关的团队信息分发到控制台以供查看。到目前为止,该程序允许用户输入包含未格式化团队数据的文本文件。然后,它格式化该数据,将其存储并将信息打印到控制台。此时,在我的程序中,我希望用户能够根据团队名称开始其过滤。我并不一定要找到确切的答案,但会提供一些有用的提示或建议。

public static void main(String[] args) {

    Scanner keyboard = new Scanner (System.in);


    // Allow the user to enter the name of text file that the data is stored in
    System.out.println("This program will try to read data from a text file ");
    System.out.print("Enter the file name: ");
    String filename = keyboard.nextLine();
    System.out.println();

    Scanner fileReader = null;

    //A list to add results to, so they can be printed out after the parsing has been completed.
    ArrayList<LineResult> results = new ArrayList<>();

    try {
        File Fileobject = new File (filename);
        fileReader  = new Scanner (Fileobject);

        while(fileReader.hasNext()) {        
            String line = fileReader.nextLine();// Read a line of data from text file

            // this if statement helps to skip empty lines
            if ("".equals(line)) {
                continue;
            }

            String [] splitArray = line.split(":");
            // check to make sure there are 4 parts in splitArray 
            if(splitArray.length == 4) {
                // remove spaces
                splitArray[0] = splitArray[0].trim();
                splitArray[1] = splitArray[1].trim();
                splitArray[2] = splitArray[2].trim();
                splitArray[3] = splitArray[3].trim();

                //This section checks if each line has any corrupted data
                //and then display message to the user.
                if("".equals(splitArray[0]))
                {
                    System.out.println(line + "  > The home or away team may be missing");
                    System.out.println();

                }else if ("".equals(splitArray[1])) {
                    System.out.println(line + "  >  The home or away  team may be missing");
                    System.out.println();

                }



                try {
                    // Extract each item into an appropriate variable
                    LineResult result = new LineResult();
                    result.homeTeam = splitArray[0];
                    result.awayTeam = splitArray[1];
                    result.homeScore = Integer.parseInt(splitArray[2]);
                    result.awayScore = Integer.parseInt(splitArray[3]);

                    results.add(result);
                } catch(NumberFormatException e) {
                    System.out.println(line + " > Home team score may not be a valid integer number ");
                    System.out.println("                             or it  may be missing");
                    System.out.println();
                }     
            }else {
                System.out.println(line + " > The field delimiter may be missing or ");
                System.out.println("                         wrong field delimiter is used");
                System.out.println();
            }
        }
        System.out.println();
        System.out.println();

        //Print out results
        System.out.println("Home team        Score       Away team           Score");
        System.out.println("=========        =====       =========       =====");


        //Loop through each result printing out the required values.

        //TODO: REQ4, filter results based on user requested team

        try (BufferedReader br = new BufferedReader(new File(filename));
                 BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"))) {
                String line;
                while ((line = br.readLine()) != null) {
                    String[] values = line.split(" ");
                    if (values.length >= 3)
                        bw.write(values[0] + ' ' + values[1] + ' ' + values[2] + '\n');
                }
            }

        for (LineResult result : results) {
            System.out.println(
            String.format("%-15s    %1s         %-15s       %1s",
                    result.homeTeam,
                    result.homeScore,
                    result.awayTeam,
                    result.awayScore));
        }                   
    // end of try block
    } catch (FileNotFoundException e) {
        System.out.println("Error - File does not exist");
        System.out.println();
    }
}

//Data object for holding a line result
static class LineResult {
    String homeTeam, awayTeam;
    int homeScore, awayScore;}
}

0 个答案:

没有答案