对于我的作业,我必须列出所有课程(只是课程代码),这些课程在给定的一天在给定的建筑中上课,因此课程的任何部分都在给定的时间之间。每门涉及的课程应该只列出一次,即使它有几节课。我什么都做了,只列出了一次课程,即使它有好几节课。如何忽略文件中的重复字符串?
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("=========================");
}
2条答案
按热度按时间ws51t4hk1#
您可以将所有字符串标记放入集合中,并在进一步处理之前检查该标记是否包含在集合中,如下所示:-
dsf9zpds2#
您可以将课程放在一个集合中并在其上循环,因为集合总是包含唯一的值。