You cannot use the for loop because you don't know when the loop will stop,
or better said when the user will stop.
Therefor, stick with the while loop, but still use the integer i you were using
with the for loop. Write something like :
CdRecord array = new CdRecord;
int i = 0;
while (menu == 1 && i < 5)
array[i] = new CdRecord();
array[i] . artist_name =
JOptionPane.showInputDialog("Enter Artist Name");
array[i] . album_name =
JOptionPane.showInputDialog("Enter Album Name");
array[i] . no_of_tracks =
Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Tracks"));
In short, you should add the integer i you already used in the for loop.
This way , you keep track of the number of the element of the array you are
using. In the while loop you should check if the value of i is less than 5 :
the reason of this is that you've got only 5 elements in your array. And arrays
start from 0 : so element 0 till 4 is valid. Element 5 would mean the sixth
element in your array and you don't have space for that sixth element.
As soon as you have put the values of the user in your CDRecord, you add
1 to the value of i : this means you're going to the next element in your array.