Help for a begginer in java

Thunderbird

Newcomer
Hey guys, I'm taking a first year uni course in java and I'm stuck on an assignment and was wondering if someone here could help me out.

The problem involves taking courses and their prerequisites and printing out the earliest term they can be taken. Here's some sample input:

CS2 CS1
CS2 Math100 Math150
Math200 Math100 Math150
CS212 Math100 Logic112 Phil112
CS212 CS2
CS212 Engl100 Engl150

The first two lines show that before doing CS2 you should do CS1 and
either Math 100 or 150. Math200 also requires one of Math 100 or 150.
Before doing CS212 a person should complete CS2 and one of, Logic112,
Phil112, or Math100, and one of Engl100 or Engl150.

the output should look like this:
CS1 Engl100 Engl150 Logic112 Math100 Math150 Phil112
CS2 Math200
CS212

and I get this:
CS1 Engl100 Engl150 Logic112 Math100 Math150 Phil112
Math200
CS2
CS212

I'll try and explain what it is I think I'm doing so hopefully that'll help me find out whats wrong.

First I use a stringtokenizer to read the input and then check to see if the first course is contained in a map that will hold a set for each course. If it's not, then a new set with that course name is added to the map. The remaining tokens on that line are read in and checked again to see if they are allready in the map, otherwise they're added to a new set.

To print it out I iterate through the map and find each set with zero prerequisites, and add them to a temporary arraylist that will be the first line of output. This works ok but the next lines are the problem. When I iterate through the map again to find those courses with prerequisites, only those courses that haven't allready been printed should be.

Can anyone suggest changes that will help me get the required output?
Thanks, and here's my code so far.

Code:
import java.util.*;
import java.io.*;

public class T8 {
    public static void main (String args[]) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String course, line;
        Map m = new TreeMap();
        Set s;
 
        while ((line = br.readLine())!=null){
            StringTokenizer st = new StringTokenizer(line);
            String a =null;      
            String next;             
            String name = st.nextToken();
            s = (TreeSet) m.get(name);   //ckeck first, if not in map, make 

            if ( s == null){ 
                m.put(name, s = new TreeSet()); //new set
            }
            while( st.hasMoreTokens() ) { //all remaining tokens, add to set 
                                          //from first token
                a = st.nextToken();
                s.add(a);  
                TreeSet s1 =(TreeSet)m.get(a);
                if( s1 ==null){
                    m.put(a, new TreeSet());              
                }
            }

            
        }
        TreeSet temp;        
        ArrayList output = new ArrayList();
        ArrayList arraytemp = new ArrayList();
        int inttemp =0;
        while(m.size()>0){                     // inttemp <20){
            for(Iterator it = m.keySet().iterator(); it.hasNext();){
                course = (String)it.next();          //course is key from map dictionary
                temp = ((TreeSet)m.get(course));   //temp is set that matches key in map
                if ((temp.size()) == inttemp){      //if this set has no prerequisites, add to arraylist
                    if(!arraytemp.contains(course)){ 
                        output.add(course); 
                    }
                    it.remove();         // remove this course from the map
                }

            }
            if(!output.isEmpty()){
              System.out.println("output array contains" + output);        
             }
             inttemp++;
            arraytemp.addAll(output);  
            output.clear();


        }  
 
    }
}
 
each key contains a treeset with the listed prerequisites, so because you are not removing the course from the treesets, your output is off

remove the course from all the treesets and the keyset as well, and it should work correctly

another thing, you dont really need to use the "arraytemp" that you have (unless i overlooked something)
 
Back
Top