Reputation: 129
I have a file pacs.txt
with:
a,b,c,d
Then I have another file members.txt
with:
sara a,c
brad d,a
tammy c,b
I have to basically print out
a sara, brad
b tammy
c sara tammy
I have written a code that tries to do this. I went in and asked for help from my TA and she says I'm really close I'm just messing up my Treemap. Can someone please help me figure out what I'm doing wrong. List A is pacsFile and List B is membersFile.
import java.io.*;
import java.util.*;
// N O C O M M A N D L I N E A R G S!
// A L L F I L E N A M E S M U S T B E H A R D C O D E D
public class Pacs
{
public static void main( String args[] ) throws Exception
{
BufferedReader pacsFile = new BufferedReader( new FileReader( "pacs.txt" ) );
BufferedReader membersFile = new BufferedReader( new FileReader( "members.txt" ) );
TreeMap<String,String> Map = new TreeMap<String,String>();
ArrayList<String> acroymn = new ArrayList<String>();
String group;
String people;
while((group = pacsFile.readLine()) != null)
{
acroymn.add(group);
}
Collections.sort(acroymn);
pacsFile.close();
while((membersFile.ready()))
{
ArrayList<String> members = new ArrayList<String>();
people = membersFile.readLine();
String [] peoples = people.split(" ");
members.add(peoples[0]);
for (int i = 1; i< peoples.length; i++)
{
Map.put(peoples[i],peoples[0]);
}
}
membersFile.close();
for(String acro: acroymn)
{
String name = "";
for(String mem: Map.keySet())
{
for(String S: Map.get())
{
if(acro.equals(S))
{
name = name+ " " + mem;
}
}
}
System.out.println(acro + " " + name);
}
} // END MAIN
} // CLASS
Upvotes: 1
Views: 371
Reputation: 37730
I see several problems in the way you read the information in the files.
"pacs.txt"
First, I feel like the acronym
list will only contain a single element with all the letters:
while((group = pacsFile.readLine()) != null)
{
acroymn.add(group);
}
If the first line of your file actually contains all letters separated by commas (as you stated), then this will only add a single element to the list: the string "a,b,c,d"
.
You should probably use group.split(",")
and then add its elements to your list.
"members.txt"
Second, it seems that you loop on people
without accessing different indices:
for (int i = 0; i< peoples.length; i++)
{
members.add(peoples[0]);
Map.put(acroymn,peoples[0]);
}
You always access 0.
You're currently storing the content of one file in a data structure, the content of the other file in another data structure, and then putting everything together.
You should probably follow @LuiggiMendoza's advice here, and use a Map<String, List<String>
. Use this structure directly in both steps of the process.
Here are some hints about what you can do with it.
"pacs.txt"
Add all elements as keys of your map using:
map.put(element, new LinkedList<String>());
This way, all your keys will be present, even with noone associated with. Moreover this will initialize the lists of people to empty lists that you will just need to complete.
"members.txt"
For each person, obtain the list corresponding to each key of the line using:
List<String> list = map.get(key);
And add your person to it:
list.add(person);
Upvotes: 0
Reputation: 85779
I think the problem is how you're storing the data. It would be better to store it like this:
TreeMap<String, List<String>> data = new TreeMap<String, List<String>>();
Where the elements in first file (a, b, c, d) will be the keys and the names in second file (sara, brad, tammy) will be the values to be added for each entry of the map.
At the end, just print the key-value pair that don't have any null
value.
Upvotes: 1