Reputation: 133
I want to use the MaxFriends method to find the person with the most friends. Printing the number of friends from the linked list is easy enough but since I clear it after each iteration of the while loop I don't know how to compare the values at the end...
I think the problem could be simplified if I just found the line with the most 'tokens' or in this case strings. Is there a way to do this?
I'm reading in a text file (to create a linked list).
Text file looks like this:
john, peter, maria, dan, george, sonja
maria, nell, ted, don, matthew, ann, john, george
fred, steve
ann, tom, maria
Code thus far:
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() {
}
}
Upvotes: 2
Views: 324
Reputation: 1277
I changed portion of your code into somewhat like below:
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();
}
After while loop terminates, you can get the leader with the most friends.
Upvotes: 1
Reputation: 9157
If I understand the problem correctly, you just need to keep track of who has the most friends so far, and compare that to the next candidate for each line. Stuffing everything into a map or heap seems unnecessary.
By the way, the parsing you're doing is very simple and doesn't need a scanner:
String[] friends = line.split(",\\s*");
System.out.printf("%s has %d friends\n", friends[0], friends.length - 1);
Upvotes: 2
Reputation: 2828
Why not use a Hashmap for storing the information on a per friend basis
Map<String, List<String>> friends = new HashMap<String, List<String>>();
After each iteration use the friends name as the key in the hashmap and then add the linked list to the map as the value.
Then in maxFriends you will be able to go through the keys and get the values and verify which list had the greatest size and thus the most friends.
Upvotes: 0