1

我有一个 java 程序正在扫描包含以下信息的文本文件:

高级武术文凭
(伯班克斯忍者学校)
Adkins、Scott 72
Black、Jack 44
Carradine、David 81
Chan、Jackie 75
Chow、Stephen 77
Chow、Yung-Fat 79
Jaa、Tony 71
Lee、Bruce 76
Li、Jet 76
Norris、Chuck 71
Oedekerk, Steve 58
Reeves, Keanu 62
Uwais, Iko 75
Yen, Donnie 80
Yeoh, Michelle 79
Zhang, Ziyi 75

Pilot (Starfleet Academy)
Archer, Jonathan 71
Janeway, Kathryn 74
Kirk, James T. 79
Picard, Jean-Luc 85
派克,克里斯托弗 80
莱克,威廉 79

进阶动作英雄(迪士尼 U)
阿拉丁 91
本尼迪克特、朱利叶斯 45
布鲁尔、戈迪 82
柯南 60
鸭、唐纳德 40
不可思议、85
矩阵先生、约翰 64
老鼠、米奇 51
欧文斯、雷 82
波平斯、玛丽 85
奎德、道格拉斯 75
理查兹、本 68
辛巴 80
斯莱特、杰克 80
终结者、90
塔利、路易 DNF

服装、波伊丝和

格蕾丝比伯、贾斯汀 33
布洛克、桑德拉 80
西科内、麦当娜 60
赛勒斯、麦莉 40
加加、50
劳珀夫人、辛迪 55
米娜、妮琪 45
萨基西安, 雪儿 75

就是这样,伙计们!

到目前为止,这是我扫描文本文件并返回格式化值的代码:

public static void main(String[] args) throws Exception {
    Scanner fileScanner = new Scanner(new File("lists.txt"));
    String line = fileScanner.nextLine().trim();

    String programName;
    String schoolName;
    String topStudent;
    int topGrade;



    while (!line.equals("That's All, Folks!")) {
        //extract a program's information
        System.out.println("");
        System.out.println(line.trim());
        line = fileScanner.nextLine().trim();
        line = line.replaceAll("\\s+", " ");
        System.out.println("");
        while (!line.isEmpty()) {

            //deal with one student                
            System.out.println(line);
            line = fileScanner.nextLine().trim();
            line = line.replaceAll("\\s+", " ");
        }
        //displays summary information for a program    
        line = fileScanner.nextLine().trim();
    }
}

我应该检查这些字符串(有些有一个,有些有两个名字。一个有 DNF 而不是等级)。我将如何只返回每个程序中每个人的姓名和最高成绩,如下所示:

高级武术文凭
(伯班克斯忍者学校)
大卫卡拉丁

飞行员
(星际舰队学院)
让-吕克·皮卡德

进阶动作英雄
迪士尼 U)
阿拉丁

着装、风度和格蕾丝
桑德拉·布洛克

编辑:添加变量

4

2 回答 2

2

So a good place to start would be

line = line.replaceAll(", ", ",");
String[] split = line.split(" ");

in the split array your going to have [name, mark] if its a person or just [name] if its the name of the academy.

于 2013-10-16T19:29:20.000 回答
0

So it looks like at this point you're just printing Strings and then tossing them out.

It also looks like you're dealing with several different types of information (academy, names, grade)

This will be hard to deal with later on if you're just using Strings. I would start by building a couple of data structures to hold the different kinds of information as you parse the file.

So perhaps you could have an Academy class that has many Student objects, and each Student has a name, etc.

于 2013-10-16T19:29:06.197 回答