将文件扫描结果一起添加

时间:2015-01-04 14:34:12

标签: java java.util.scanner

我有一个小程序,可以从文件中读取足球队的得分,并且只输出有效格式的线。然后它将目标加在一起并显示总数。我将如何仅显示结果并仅为用户指定的一个团队添加目标?

以下是我的代码的一部分:

System.out.println("Would you like to generate output in (H)TML or (P)lain text?");
String input = scanner.nextLine();

System.out.println("Select a specific team (<enter> to show all teams):");
String teaminput = scanner.nextLine();

        FileWriter fwriter = new FileWriter("football.html");
        BufferedWriter writer = new BufferedWriter(fwriter);




        if (input.equalsIgnoreCase("H")){

            writer.write("<html><head><title>Football Scores</title></head><body><h1>Football Scores</h1><table border=\"1\">");
            writer.write("<th>Team 1</th><th>Team 2</th><th>Team 1 Score</th><th>Team 2 Score</th>");
            while (fileScan.hasNextLine()){
            String line = null;

            line = fileScan.nextLine();
            String[] element = line.split (":");

                if(line.matches("\\w+?\\s?\\w+?\\s?\\w+?\\s?[:]\\s?\\w+?\\s?\\w+?\\s?\\w+?\\s?[:]\\s?\\d+?\\s?[:]\\s?\\d+?")){ //regex pattern used to only accept valid lines. 
                       count++;
                       writer.write("<tr><td>"+element[0]+"</td><td>"+element[2]+"</td><td>"+element[1]+"</td><td>"+element[3]+"</td></tr>"); //creating output format for the strings to be printed


                       sum += Integer.parseInt(element[2].trim()); 
                       sum1 += Integer.parseInt(element[3].trim());

                } else {
                invalidLines++;
                }

        }
            writer.write(result = sum + sum1);
            writer.write("<br>Total number of goals: " +result);
            writer.write("<br>"+count+" lines successfully validated");
            writer.write("<br>"+invalidLines+" lines declined.");
            writer.write("</table></body></html>");
            writer.close();

以下是输入数据的示例:

Leeds United : Liverpool : 1 : 2
Chelsea :  Manchester City : 1 : 1
Aston Villa : Middlesbrough : 3 : 1

0 个答案:

没有答案