Note that there are some explanatory texts on larger screens.

plurals
  1. POlooping through array - only first index printing
    primarykey
    data
    text
    <p>I want to loop through an array and print any values that are not null, here is the code I am trying to use:</p> <pre><code>for (int i = 0; i &lt; 10; i++) { if (studentNamesArray[i] != null) { studentFound = true; System.out.println("Which student would you like to delete?"); System.out.println(i + ": " + studentNamesArray[i]); int studentChoice = input.nextInt(); } } </code></pre> <p>Array:</p> <p><code>static String[] studentNamesArray = new String[10];</code></p> <p>The problem is that it is only printing out <code>index[0]</code>. How can I fix this?</p> <p>EDIT:</p> <p>Here is my full code:</p> <pre><code> static void deleteStudent() { boolean studentFound = false; for (int i = 0; i &lt; 10; i++) { if (studentNamesArray[i] != null) { studentFound = true; System.out.println("Which student would you like to delete?"); System.out.println(i + ": " + studentNamesArray[i]); } int studentChoice = input.nextInt(); for (i = 0; i &lt; 10; i++) { for (i = studentChoice + 1; i &lt; studentNamesArray.length; i++) { studentNamesArray[i - 1] = studentNamesArray[i]; } nameArrayCount = nameArrayCount - 1; studentNamesArray[studentNamesArray.length - 1] = null; for (i = studentChoice + 1; i &lt; 9; i++) { for (int y = 0; y &lt; 3; y++) { studentMarksArray[i - 1][y] = studentMarksArray[i][y]; } } markArrayCount = markArrayCount - 1; for (int y = 0; y &lt; 3; y++) { studentMarksArray[9][y] = 0; } } } if (!studentFound) { System.out.println("There are no students stored"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload