Home > Back-end >  My array is not reaching the last element of my array [closed]
My array is not reaching the last element of my array [closed]

Time:10-01

public int numProjectsGraded(String firstName, String lastName) {
     
      if(firstName == null || firstName.isEmpty() == true || lastName == null || 
              lastName.isEmpty() == true)
          
          return -1;
      
      String name = firstName   " "   lastName;
     
      for(int i = 0; i < taList.length-1; i   ) {
       
          if(taList[i].fullName().compareTo(name) == 0) {
              
              return taList[i].getProjects(); 
          }
   }
      return -1;
  }

The premise of my project is that there are courses and there are TAs assigned to each course. Every course has its own TA array. We manage their projects, hours, and salaries here. For this method, I need to return the amount of projects a certain TA has graded. In order to get the TA, I made a for loop to loop through the array (I have to use array not ArrayList) and compared every name of the TAs to the name input to the method. Everything works fine except for the fact that my loop seems to not be able to access the last element of the array. I may have been looking at it too long and made it way more complicated than it needed to be but I need some help. Thanks!

CodePudding user response:

for(int i = 0; i < taList.length-1; i   )

means if i is equal to taList.length-1, don't enter to the block.

Hence, consider changing it to:

for(int i = 0; i < taList.length; i   )

CodePudding user response:

The iteration final condition should be i < taList.length instead of i < taList.length -1

  • Related