Colin747
Colin747

Reputation: 5013

else statement printing multiple times - array loop

I want my code to loop through an array and only give the user an option to delete a student only if there are values in the array. If all the array values are null then I want it to print out a message. The problem is that my message is printing out multiple times for each null element in the array.

My code:

  static void deleteStudent() {
    for (int i = 0;i < 10;i++) {
      if (studentNamesArray[i] != null) {
        System.out.println("Which student would you like to delete?");
        System.out.println(i + ": " + studentNamesArray[i]);
        int studentChoice = input.nextInt();
        for (i = studentChoice + 1;i < studentNamesArray.length; i++) {
          studentNamesArray[i-1] = studentNamesArray[i];
        }
        nameArrayCount = nameArrayCount -1;
        studentNamesArray[studentNamesArray.length - 1] = null;
        for(i = studentChoice + 1;i < 9;i++) {
          for(int y = 0;y < 3;y++) {
            studentMarksArray[i-1][y] = studentMarksArray[i][y];
          }
        }
        markArrayCount = markArrayCount - 1;
        for(int y = 0;y < 3;y++) {
          studentMarksArray[9][y] = 0;
        }
      } else {
          System.out.println("There are no students stored");
      }
    }
  }

Upvotes: 0

Views: 1704

Answers (4)

C. S.
C. S.

Reputation: 813

In your for loops, you use the same variable i, in which it overrides the original variable used in the for loop.

Try something like this:

static void deleteStudent() {
    for (int i = 0;i < 10;i++) {
      if (studentNamesArray[i] != null) {
        System.out.println("Which student would you like to delete?");
        System.out.println(i + ": " + studentNamesArray[i]);
        int studentChoice = input.nextInt();
        for (int j = studentChoice + 1;j < studentNamesArray.length; j++) {
          studentNamesArray[j-1] = studentNamesArray[j];
        }
        nameArrayCount = nameArrayCount -1;
        studentNamesArray[studentNamesArray.length - 1] = null;
        for(int k = studentChoice + 1;k < 9;k++) {
          for(int y = 0;y < 3;y++) {
            studentMarksArray[k-1][y] = studentMarksArray[k][y];
          }
        }
        markArrayCount = markArrayCount - 1;
        for(int z = 0;z < 3;z++) {
          studentMarksArray[9][z] = 0;
        }
      } else {
          System.out.println("hi");
      }
    }
}

Although, I am not sure what you are trying to print out, I made adjustments to the other variable names based on my intuition; the result might not be exactly as resulted, but I am sure you can make further adjustments with the variable names so that you are not over-riding each other and causing excessive loops.

Upvotes: 0

polis
polis

Reputation: 805

I suggest to use 'boolean' variable, lets name it 'flag'. Initialize it as 'false'. If you found not null element in array, when set 'flag = true'. After 'for' loop check 'if (!flag) System.out.println ("There are no students.");'.

Upvotes: 0

sjr
sjr

Reputation: 9875

The else block runs in each loop iteration. If you want to run it only once at the end, do something like this:

boolean studentFound = false;

for (int i = 0;i < 10;i++) {
    if (studentNamesArray[i] != null) {
        studentFound = true;
...

Then after the for:

if (!studentFound) {
    System.out.println("There are no students stored");
}

Upvotes: 1

Vineet Kosaraju
Vineet Kosaraju

Reputation: 5840

Here is how I would determine if all the elements are null. The reason yours prints it out for each null element is that the print statement is inside the for loop.

boolean allNull = true; // default is that everything is null
for(int i = 0; i < studentNamesArray.length; i++) { // iterate through entire array
    if(studentNamesArray[i] != null) { // if even 1 of them is not null
        allNull = false; // set allNull to false
        break; // and break out of the for loop
    }
}

if(allNull) System.out.println("There are no students stored!");

Upvotes: 0

Related Questions