从文本文件中筛选特定团队并显示结果

siv3szwd  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(180)

我希望我的程序允许用户输入团队名称,并基于该名称将相关团队信息分发到控制台以供查看。到目前为止,该程序允许用户输入包含未格式化团队数据的文本文件。然后对数据进行格式化、存储并将信息打印到控制台。在我的程序中,我希望用户能够根据团队名称启动筛选。我不一定要寻找一个确切的答案,但一些有用的提示或建议将不胜感激。

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;}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题