mardi 28 avril 2015

Ignore duplicate in string and print string once

Pretty much for my assignment I have to List all the courses (just the course code) that have classes in a given building on a given day such that any part of the class is between the given times. Each course involved should only be listed once, even if it has several classes. I have done everything except listing the course once, even if it has several classes. How do I ignore duplicate strings from a file?

public void potentialDisruptions(String building, String targetDay, int targetStart, int targetEnd){
    UI.printf("\nClasses in %s on %s between %d and %d%n",
               building, targetDay, targetStart, targetEnd);
   UI.println("=================================");


   boolean containsCourse = false;
   try {
      Scanner scan = new Scanner(new File("classdata.txt"));

       while(scan.hasNext()){

       String course = scan.next();  
       String type= scan.next();   
       String day = scan.next();   
       int startTime = scan.nextInt();   
       int endTime = scan.nextInt();   
       String room = scan.next();


        if(room.contains(building)){  
           if(day.contains(targetDay)){
           if(endTime >= targetStart){
           if( startTime<= targetEnd){

           UI.printf("%s%n", course);   
           containsCourse = true;
        }
        }
        }     
       }
      }
      if(!containsCourse){
          UI.println("error");
        }
    }
    catch(IOException e){
       UI.println("File reading failed");
    }
   UI.println("=========================");

}

Aucun commentaire:

Enregistrer un commentaire