0

I'm in a situation where I first declared a array without a size. Then later give the array a size but in a if statement. In another if statement i try to use .length for the array but it does not work. For some background info my program is like a school and I have teachers which are objects. The teachers have courses (the array) and in the first if statement it gathers how many courses that teacher has. In the other if statement it tried to remove any eligible courses.

String[] courses;

if (strInput=="y")
{
    System.out.println("Enter number of courses");
    int courseLoop=Integer.parseInt(MyInput.readLine()); //make this the numberfor the loop to continue and number of course codes

    courses = new String [courseLoop];
    for (int y=0;y<=courseLoop;y++)
    {
        System.out.println("Enter course code");
        courses[y]=MyInput.readLine(); //enters the course code in
    }

    for(int i = 0; i < courses.length; i++) //starts checking for course addtion
    {
        if (teacher[x].addCourse(courses[i]))
        {
            System.out.println(courses[i] + " added."); //if course eligable added
        }
        else
        {
            System.out.println(courses[i]+ " cannot be added."); // if ourse uneligable not added
        }
    }
}

if (teacher[x].getName()==strInput) // if the name inputed matched then does the process
{
    for(int i = 0; i<courses.length; i++)
    {
        if(teacher[x].removeCourse(courses[i]))
        {
            System.out.println(courses[i] + " removed.");
        }
        else
        {
             System.out.println(courses[i] + " cannot be removed.");
        }
        correctEntry=true;
    }
}
ormim
  • 9
  • 1
  • 1
    Well, for one, this, `if (strInput=="y")`, is wrong. Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Oct 20 '19 at 22:22
  • So instead, that line should be: `if (strInput.equals("y"))`, assuming that you're sure that strInput is not null. – Hovercraft Full Of Eels Oct 20 '19 at 22:22
  • 1
    For more on this, check out [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Hovercraft Full Of Eels Oct 20 '19 at 22:23
  • Thanks for that oversight but this does not help with my issue where my for loop with the course.length and the it tells me that it has not been initialized – ormim Oct 20 '19 at 22:25
  • So you have a [Variable might not have been initialized error](https://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error). That is important information and should be in your question. Please look at the link given here for more on this. Then initialize those variables *before* using them, and *not* within an if block. – Hovercraft Full Of Eels Oct 20 '19 at 22:29
  • I think it has to deal with my array size. When i first introduced it has no specific array size then in a later if statement it gets a size. However for where its saying its not initialized i used array.length and it gives me that error (this is in a another different if statment). – ormim Oct 20 '19 at 22:42
  • 2
    After this declaration ```String[] courses;``` you do not have any array. You have an uninitialized variable that is capable of holding a reference to an array (and the array is the thing that 'has' a size, not the variable). There are code paths that never assign a value (an array reference) to the variable, thus the 'uninitialized variable' warning. –  Oct 20 '19 at 22:49

2 Answers2

0

The simplest fix for this specific uninitialized variable problem is to always initialize your array variable. And examination of the logic suggests that an empty array is the most appropriate value.

String[] courses = new String[0];

That solves the 'variable not initialized' issue.

Then, if we obey the clause that depends on strInput equaling "y", we'll replace the empty array with one of the size you want.

When we get to the clause depending on strInput being the name of teacher 'x', courses.length will be zero or else the number of courses. Either way it works correctly, i.e., we do not attempt to dereference an uninitialized variable nor do we attempt to access an uninitialized or undefined array element.

Mind you, the overall logic still seems confused. Unless teacher 'x' happens to have the name "y", you can't execute both of the bodies of the main 'if' statements, since the first one depends on the string "y" and the second depends on the teacher's name.

(Standard indentation would have helped, too)

0

I am assuming that strinput Y means you are initializing the teacher's courses. And when strinput is the teacher name you want to process the teacher's courses that were previously initialized.

When in the if statement where the teacher name matches the input you most likely intended to use the teacher's courses that were previously initialized.

To do this you would need something like courses=teacher [x]. getCourses() as the first statement. Then the courses variable would be set.

fedup
  • 1,209
  • 11
  • 26