http://puu.sh/l3owc/e16f0fe76f.png (Instructor Prompt)
Basically, I am trying to let the user exit the loop by typing -1 for the song title input. For some reason, it wont work for me, and it just keeps the value -1 as the song name. The other problem I have is, I am trying to print 'all remaining songs' like the prompt says to, after every song has been input. For me, it just erases the previous one and shows the most recent song title and length, when I want it to show all (is that what my prompt means) songs entered.
Then, the user gets to remove songs, and after that, a report should show. Should I be using some sort of report string that will keep adding to it? Not sure how to do that...I am close to figuring this out, just need some help. Thank you very much kind people of this website
import javax.swing.JOptionPane;
public class IT106_Playlist {
public static void main(String[] args) {
final int MAX_SONGS = 106;
int totalDuration = 0;
int numSongs = 0;
boolean exitVar = false;
int i = 0;
String[] songTitles = new String[MAX_SONGS];
int[] songLengths = new int[MAX_SONGS];
while (exitVar == false && numSongs <= songTitles.length) {
do {
songTitles[numSongs] = JOptionPane.showInputDialog(null,"Enter a song name, or type -1 to exit");
if (songTitles[numSongs].equals("")) {
JOptionPane.showMessageDialog(null,"Error: Please enter a valid song name, or type -1 to exit");
} else if (songTitles[numSongs].equals("-1")) {
exitVar = true;
}
} while (songTitles[numSongs].equals(""));
do {
try {
songLengths[numSongs] = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a song length, e.g. 4."));
if (songLengths[numSongs] > 0) {
totalDuration += songLengths[numSongs];
} else {
songLengths[numSongs] = -1;
JOptionPane.showMessageDialog(null,"Error: please enter a valid song length, e.g. 4.");
}
} catch (NumberFormatException e) {
songLengths[numSongs] = -1;
JOptionPane.showMessageDialog(null, "Error: please enter a valid song length, e.g. 4.");
}
} while (songLengths[numSongs] <= 0);
boolean addMore = true;
while ((numSongs <= MAX_SONGS) && (addMore == true)) {
JOptionPane.showMessageDialog(null, "Song #" + (i+1) + ": " + songTitles[i] + " length: " + songLengths[i] + "\n");
i++;
if (songTitles[i] == null) {
addMore = false;
}
}
numSongs++;
}
}
}