在大多数数据的.txt文件中查找行

时间:2012-02-06 01:06:59

标签: java text-files

我想使用MaxFriends方法找到朋友最多的人。从链表中打印朋友的数量很容易,但是因为我在while循环的每次迭代后清除它,所以我不知道如何比较最后的值...

我认为如果我找到最多'令牌'或者在这种情况下字符串的行,问题就可以简化了。有没有办法做到这一点?

我正在阅读文本文件(以创建链接列表)。

文本文件如下所示:

john, peter, maria, dan, george, sonja
maria, nell, ted, don, matthew, ann, john, george
fred, steve
ann, tom, maria

到目前为止的代码:

import java.util.*;
import java.io.*;

import javax.swing.JFileChooser;
public class Test {

public static void main(String[] args) {
    LinkedList<String> list = new LinkedList<String>();

    LinkData ld1 = new LinkData();
    JFileChooser chooser = new JFileChooser(".");
    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: ");

        // open and read file:
        Scanner scanner = null;
        try {
            scanner = new Scanner(chooser.getSelectedFile());
        } catch (IOException e) {
            System.err.println(e);
            error();
        } 

        if (scanner == null)
            error();


        while (scanner.hasNextLine()) {
            int friendCount = 0;
            String line = scanner.nextLine();
            Scanner lineScan = new Scanner(line);
            lineScan.useDelimiter(", ");
            // System.err.println("The line that was scanned: " + line);

            String leader = lineScan.next();    {

            while (lineScan.hasNext()) {
                list.add(lineScan.next());
                friendCount++;
            }

            System.out.println("Friend Leader: " + leader + "\n" + 
                    "\tFriends include: " + list.toString() + "\n" +
                    "\tNumber of Friends: " + list.size() + "\n");
            }   list.clear(); 

        }  
    }
        }

private static void error() {
    System.err.println("An error has occurred: bad data");
    System.exit(0);
}

public void maxFriends() {

}
}

3 个答案:

答案 0 :(得分:2)

如果我正确理解了问题,您只需要跟踪到目前为止最多的朋友,并将其与每行的下一个候选人进行比较。将所有内容填充到地图或堆中似乎是不必要的。

顺便说一下,你正在做的解析很简单,不需要扫描仪:

String[] friends = line.split(",\\s*");
System.out.printf("%s has %d friends\n", friends[0], friends.length - 1);

答案 1 :(得分:1)

我将部分代码更改为如下所示:

int maxFriendCount = 0; // added by me
String maxLeader = null; // added by me
while (scanner.hasNextLine()) {
    int friendCount = 0;
    String line = scanner.nextLine();
    Scanner lineScan = new Scanner(line);
    lineScan.useDelimiter(", ");
    // System.err.println("The line that was scanned: " + line);

    String leader = lineScan.next(); 

    while (lineScan.hasNext()) {
        list.add(lineScan.next());
        friendCount++;
    }
    // Added by me
    if(friendCount > maxFriendCount)
    {
        maxFriendCount = friendCount;
        maxLeader = leader;
    }
    System.out.println("Friend Leader: " + leader + "\n" + 
                "\tFriends include: " + list.toString() + "\n" +
                "\tNumber of Friends: " + list.size() + "\n");   
    list.clear(); 
} 

while循环结束后,你可以得到最多朋友的领导者。

答案 2 :(得分:0)

为什么不使用Hashmap以每个朋友为基础存储信息

Map<String, List<String>> friends = new HashMap<String, List<String>>();

每次迭代后,使用好友名称作为hashmap中的键,然后将链接列表作为值添加到地图中。

然后在maxFriends中,您将能够浏览密钥并获取值并验证哪个列表的大小最大,从而确认最多的朋友。