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;
}
}