-3

When running below program, I cannot reach the end of the main function... I am new to Java, and cannot find its defects. I need your help. Thanks.

import java.util.*;

class Schedule {

    public String day;
    private int startTime, endTime;

    public Schedule(String input_day, int input_start, int input_end) {
        day = input_day;
        startTime = input_start;
        endTime = input_end;
    }

    /* clashWith: to check whether this schedule clash with a Schedule called otherSchedule
     *      PRE-Condition  : input must be of Schedule type
     *      POST-Condition : return true if two Schedule clash, return false if not.
     */
    public boolean clashWith(Schedule otherSchedule) { 
        if(this.day != otherSchedule.day || this.endTime <= otherSchedule.startTime || this.startTime >= otherSchedule.endTime)
            return false;
        return true;
    }
}

class Module {

    String code;
    Schedule lecture, tutorial, lab;

    public Module(String input_code, Schedule input_lecture, Schedule input_tutorial, Schedule input_lab) {
        code = input_code;
        lecture = input_lecture;
        tutorial = input_tutorial;
        lab = input_lab;
    }

    /* count: to count number of classes(lecture, tutorial, and lab of only this Module) on day.
     *        For example: when day = "Monday", lecture is on Monday, tutorial is on Monday
     *        but lab is on Tuesday, then return 2. (lecture and tutorial are on Monday).
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public int count(String day) {
        int num = 0;
        if(lecture.day == day)
            num++;
        if(tutorial.day == day)
            num++;
        if(lab.day == day)
            num++;
        return num;
    }

    /* clashWith: to check whether this module clash with a Module called otherModule
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public boolean clashWith(Module otherModule) {
        if(lecture.clashWith(otherModule.lecture) || lecture.clashWith(otherModule.tutorial) || lecture.clashWith(otherModule.lab) )
            return true;
        if(tutorial.clashWith(otherModule.lecture) || tutorial.clashWith(otherModule.tutorial) || tutorial.clashWith(otherModule.lab))
            return true;
        if(lab.clashWith(otherModule.lecture) || lab.clashWith(otherModule.tutorial) || lab.clashWith(otherModule.lab))
            return true;
        return false;
    }
}

class Timetable {

    Vector<Module> listOfModule;

    /* checkClash: to check whether otherModule clash with one of 
     *             the modules in our timetable list.
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public boolean checkClash(Module otherModule) {
        for(Module c: listOfModule)
            if(c.clashWith(otherModule))
                return true;
        return false;
    }

    /* add: to add a new module to the timetable list.
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public void add(Module module) {
        listOfModule.add(module);
    }

    /* count: to count number of classes on day.
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public int count(String day) {
        int count_day=0;
        for(Module c: listOfModule)
            count_day += c.count(day);
        return count_day;
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num_operation;
        String code ;
        Timetable userTimetable = new Timetable();

        num_operation = input.nextInt();
        for(int i=0;i<num_operation;i++) {
            if(input.next() == "MODULE") {
                code = input.next();

                String day;
                int start, end;

                Schedule getLecSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
                Schedule getTutSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
                Schedule getLabSche = new Schedule(input.next(),input.nextInt(),input.nextInt());

                Module userModule = new Module(code, getLecSche, getTutSche, getLabSche);
                System.out.println("Reached line 162");
                if(!userTimetable.checkClash(userModule)) {
                    userTimetable.add(userModule);
                    System.out.println("Added");
                }
                else
                    System.out.println("Clashed");
            }
            else if(input.next() == "COUNT") {
                code = input.next();
                System.out.println(userTimetable.count(code));
            }
        }
    }
}
obataku
  • 29,212
  • 3
  • 44
  • 57
PUPIALEX
  • 5
  • 1
  • 3

3 Answers3

2

Found it. You're using == to compare Strings. That only compares the object reference.

Use this instead:

if (input.next().equals("MODULE"))

//...

if(input.next().equals("COUNT"))

It is also worth mentioning that this will not catch "Count", "cOunT", "Module", "module", or "mOdulE" - you may want to use equalsIgnoreCase() instead.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • That doesn't explain why it never terminates, though. – obataku Sep 09 '12 at 15:46
  • Assuming that `input.nextInt()` is a valid integer, there wouldn't be any other places that this fails to complete execution all the way through. – Makoto Sep 09 '12 at 15:50
  • right, but my point is that he stated "*I cannot reach the end of the main function*", whereas this wouldn't explain the code executing indefinitely like that. – obataku Sep 09 '12 at 15:51
  • 1
    That depends on how OP defined "end of main function". You and I see see it as when the program terminates. I see it as when the OP sees their desired output. It's *very likely*, given the code, that OP is not seeing their desired output, hence they cannot reach the end of main. – Makoto Sep 09 '12 at 15:53
  • I see, it appears OP doesn't know how to effectively communicate what behavior he's experiencing... +1 for wise intuition. – obataku Sep 09 '12 at 15:54
  • Thanks...Perhaps when I am posting I did not state my problem clearly.. Thank you guys for your prompting help! It really helps a newcomer a lot. – PUPIALEX Sep 09 '12 at 15:59
  • @Makoto, thanks for your reference link, that is very useful! – PUPIALEX Sep 09 '12 at 16:15
1

In Timetable, you are never assigning a value to listOfModule:

Vector<Module> listOfModule;

Which causes a NullPointerException. You need to assign a new object to that reference:

Vector<Module> listOfModule = new Vector<Module>();
Adam
  • 15,537
  • 2
  • 42
  • 63
-1

I think the problem is with input.next() here.

public String next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

UPDATE :
Removing the confusion for downvoters input.next() above is from statement if(input.next() == "MODULE") {

Harmeet Singh
  • 2,555
  • 18
  • 21
  • @Makoto please have a clear look `if(input.next() == "MODULE") {` – Harmeet Singh Sep 09 '12 at 16:09
  • My mistake, that's one place that it's used. However, if this didn't block and received a value, then the if statement would not be executed, since that's a reference equality, not a value equality comparison. – Makoto Sep 09 '12 at 16:10
  • @Makoto I answered this reading OP *"I cannot reach the end of the main function"*, I have been -2 haha.., – Harmeet Singh Sep 09 '12 at 16:14