Hi all,

Wonder if someone could please lend a hand,
I have a courses.txt file which is read into a Course String array
I also have a program.txt (A list of 8 courses required to pass the program)

There's a menu option to change the Program, however a condition of that is
that the new program must contain only courses from the courses.txt (aka the courses array)

I can't seem to do that, any assistance would greatly be appreciated
Here is the code where courses are entered

try
        {
            courses[0] = new course();
            for (int j = 0; j < 8; j++)
            {
                System.out.println("\n\nPlease enter the new course codes");
                System.out.print("Program " + (j+1) +"  >>  ");
                String tempCourse = input.next();
                if (courses[0].checkCourse(tempCourse) == true)
                {
                    tempCourse = programObjects[j];
                }
                else
                {
                    System.out.print("Sorry that course is a listed course");
                }
            }
        }

and the checkCourse method

public boolean checkCourse(String tempCourse)
    {
        boolean matched = false;
        for (int i = 0; i < courseArrayLength; i++)
		{
            if (courseObjects[i].equals(tempCourse))
			{
                matched = true;	//Matched Course Code
            }
            else
            {
                matched = false;
            }
        }
        return matched;
    }

After enter the first course I get "null" to the screen
which then causes it to go back to the menu, Is it to do with my courses[0] instance?

Thank you in advance!

//--------- When Course Matches break from for loop else it will make matched again false.

if (courseObjects[i].equals(tempCourse))
{
matched = true; //Matched Course Code
break;
}

Edited 5 Years Ago by Majestics: n/a

In checkCourse you continue to loop and re-set "matched" after finding a match, so it will only return true if the string matches the very last element in the array. You need to exit the loop as soon as you find a match.
Assuming courseObjects[] is some kind of variable, there's no need for the courses[0].

ps
if (courses[0].checkCourse(tempCourse) == true)
is horribly redundant. What's wrong with
if (courses[0].checkCourse(tempCourse))

Java coding standards suggest methods that return a boolean should have a name that starts "is" to make their meaning absolutely clear.
Applying all the above you could write
if (isAValidCourseName(tempCourse))

Thank you both, I did not see the break and that makes that much more clear.
I have changed the boolean names for better understanding, I had noticed that before but not implemented before, thanks James.

courseObjects[] is the array of 20 courses (from courses.txt)
It's within the Course class


The code to change the program structure (the 8 of 20 courses required)
is within the Program class.

I have fixed the above errors but I get the following output

-------------------------------
Welcome to the Graduation Checker


1 - Graduation Checker
2 - Create Student
3 - Enter/Update Results
4 - Modify The Program Structure
5 - List Current Students
6 - Exit Program


Please enter your choice  >>  4
Current courses in the program are
1: COSC1001 : Programming 1 
2: COSC1002 : Programming 2 
3: COSC1003 : Software Development 
4: COSC1004 : Hardware Installs 
5: COSC1005 : Art 101 
6: COSC1006 : Networking 1 
7: COSC1007 : Networking 2 
8: COSC1008 : PC Audio 


Please enter the new course codes
Program 1  >>  COSC1001 : Programming 1
null
-------------------------------
Welcome to the Graduation Checker

I created courses[0] in the Programming class so that i could talk to an instance of courses to determine if the course is part of the courseArray in the Course class.

In programming I have declared

private course[] courses = new course[1];

Then the method above forms part of programming

Thoughts? I'm really not sure where the "null" is coming into it

Is courseObjects a static variable in the Course class? (If not, it probably should be - ie the class has a single static variable that contains all the individual instances of Course that have been created. This is a very common pattern). If so you refer to it via the class name. Creating the course[1] array is almost certainly not a good answer to anything.

Anyway, when you get value (eg null) that you don't expect then you can put loads of temporary print statements into the code leading up to that point so you can see exactly where it's coming from.

courseObjects is not static array as my only static method is the main() in the driver class..


Course looks like

public class course
{
    private String[] courseObjects = new String[20];
    private int courseArrayLength = 20;
    
    public course()
    {
    }
    
    public void loadCourse()

If I made it static I wouldn't know how to point to it ?

My code is falling down here;

System.out.println(tempCourse + "111111");
            if (courseObjects[i].equals(tempCourse))
			{
                System.out.println(tempCourse + "33333");
                isMatched = true;	//Matched Course Code
                break;
            }

Output is such

Please enter the new course codes
Program 1  >>  TestCourse
1111111
TestCourse22222
TestCourse111111
null

So it's not reading my courseObjects correctly, it is a non static String array
being called from a non static method - Could anyone please shed light as to where my logic may be falling down? Thank you

The problem seems to be with my courseObjects array, it is empty
My code is to read it from a txt file as;

public void loadCourse()
    {
        try
        {
            FileInputStream in = new FileInputStream("courses.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            for (int k = 0; k < courseObjects.length; k++)
            {
                courseObjects[k] = br.readLine();                           
            }
            in.close();
        }
        catch (Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }
    }

But when I get to read courseObjects[k] within another method it is null


So I am recalling the data from courses.txt
But after I am finished with that method it seems to dissapear,
So how could I do this if i wanted to make it a static array, would this solve the issue?

I have worked it out - thank you to all who posted :)

It didn't have a variable to compare it against
so adding

String myTempCourse = courseObjects[i];

and then comparing

if (tempCourse.equals(myTempCourse))

I now no longer have any null values.

Edited 5 Years Ago by naffan: n/a

I know you have marked this solved, but I fear that's not the case.
Your latest "fix": having the extra variable is irrelevant. But at the same time you swapped round the equals test from
courseObjects.equals(tempCourse)
to
tempCourse.equals(courseObjects)
tempCourse is never null, and null IS a valid parameter for equals, so that gets rid of the NPEs, but it doesn't fix the underlying problem.
You need to stick with the static array. Right now you have a number of Course objects, and each one of those has its own private array of courses - ie ten courses mean ten course arrays, so it's not surprising that you keep finding course arrays with nulls in them. You need exactly ONE array, and that's what static does for you. You refer to it within the class by its name, just like any other variable.

This question has already been answered. Start a new discussion instead.