0

I'm working on a project where I have to test to make sure certain code will meet user expectations. The only part I was supposed to do is write the code for GetCourseByCourseID. Everything else was given to me. I've written the code and run the program. The program runs, but it doesn't display anything. What do I need to do?

public class Course {       
public Course(String id, String title, int creditHours, String description, String prerequisiteCourse)
    {
        this.CourseID = id;
        this.CourseTitle = title;
        this.CreditHours = creditHours;
        this.Description = description;
        this.PrerequisiteCourse = prerequisiteCourse;
    }


    public String CourseID;
    public String CourseTitle;
    public int CreditHours;
    public String Description;
    public String PrerequisiteCourse;
}


public class CourseList {
public Course[] CourseArray =
    {
    new Course ("CIS 400", "OO Analysis & Design", 4, "Important class", "CIS 110") ,
    new Course ("CIS 150A" , "VB.NET Programming", 4, "Good Introduction to programming", "CIS 100") ,
    new Course ("CIS 150B", "C# Programming with labs", 4, "Follow-up to CIS 100", "CIS 100")
    };


public Course GetCourseByCourseID(String id)
{
    for (Course course : CourseArray)
        if (course.CourseID == id)
            return course;
        return null;
}
}


public class CourseListTest {
    public static void main(String[] args)
{
    GetCourseByCourseIDTestWhenCourseExists();
    GetCourseByCourseIDTestWhenCourseDoesNotExist();
}


public static void GetCourseByCourseIDTestWhenCourseExists()
{

    CourseList myCourseList = new CourseList();
    Course myCourse = myCourseList.GetCourseByCourseID("CIS 400");
    if (myCourse.CourseID != "CIS 400")
        System.out.println("ERROR - GetCourseByCourseIDTestWhenCourseExists(): Returned CourseID Not equal (CIS 400)");
}

public static void GetCourseByCourseIDTestWhenCourseDoesNotExist()
{

    CourseList myCourseList = new CourseList();
    Course myCourse = myCourseList.GetCourseByCourseID("CIS 101");
    if (myCourse != null)
        System.out.println("ERROR - GetCourseByCourseIDTestWhenCourseDoesNotExist(): should have returned null");
}
}

This is for a class. I asked my professor why it wasn't showing up and he said he wasn't familiar with java and that I should use visual basic. I personally hate visual basic (bad experience with it in a prior class) and I would love to know how it works in java! I have looked everywhere for 2 hours and not found anything! Please help!

3 Answers3

0

One of your problems is the line if (course.CourseID == id), which isn't comparing Strings correctly. You need to use equals, not == for String comparison, because == just checks whether two Strings are the actual same object in memory.

if (course.CourseID.equals(id))

This is explained in depth at How do I compare strings in Java?

You have the same problem on the line that says if (myCourse.CourseID != "CIS 400")

Try fixing those, and post a comment if your program still doesn't work.

Community
  • 1
  • 1
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0
course.CourseID == id

myCourse.CourseID != "CIS 400"

Change these two lines to:

course.CourseID.equals(id)

!myCourse.CourseID.equals("CIS 400")

See if you can get what you want.

Basically, you want to use String#equals to check for string equality instead of using ==.

Terry Li
  • 16,870
  • 30
  • 89
  • 134
-1
public Course GetCourseByCourseID(String id){
    for (Course course : CourseArray){
        if (course.CourseID.equals(id)){
            return course;
        }
    }
    return null;
}

What you were doing was returning null on the first course before you could loop through the others.

john01dav
  • 95
  • 1
  • 4
  • No, this isn't correct. Look again, and pay attention to the absence of `{` characters where you might have expected to see them. – Dawood ibn Kareem Oct 21 '13 at 01:23
  • Yes, I cannot believe that I missed that :P. Perhaps they were in the wrong place (or he just forgot them causing the error (if he is using some obscure compiler that just assumes corrections)). – john01dav Oct 21 '13 at 01:27
  • The OP's code compiles. They said "The program runs, but it doesn't display anything". I see you've now fixed your code so that it compiles too, but when I posted my earlier comment, it didn't. – Dawood ibn Kareem Oct 21 '13 at 02:00
  • I changed the equals to the proper java code (which is what screwed me up... what was given to me was wrong) but I still can't get a pop up to display the messages from the CourseListTest class. I actually finished the assignment in vb and what was missing there was the ELSE part in the methods in the CourseListTest class. (It's just annoying because they "give" us already written code... but it's wrong? So we're still double checking the whole thing anyway. Guess it's a lesson in debugging haha) – Bethany Brown Oct 21 '13 at 02:48