1

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++; 
     }
   }
  }
1
  • the link is broken Commented Aug 17 at 7:15

1 Answer 1

1

You only set exitVar = true but it's still going to do all the things that you wrote below. If you want it to stop immediately you'll either have to check whether existVar is already true all the time or you could actually abort further processing of the loop with break and a label:

    songLoop: while (numSongs <= songTitles.length) {
        do {
            ...
            } else if (songTitles[numSongs].equals("-1")) {
                break songLoop;
            }
      ...

That way nothing within the songLoop will get executed after the program hits the break songLoop command.

And if you don't want that songTitles[numSongs].equals("-1") is no longer the case after that line, you'll have to overwrite that value or not write it in there in the first place (instead in some temporary variable, and from there into the array)

Sign up to request clarification or add additional context in comments.

1 Comment

I see. I am not allowed to use break on this homework. What can I use instead? Also, I just want to make sure I am reading this correctly: I am to display all songs entered each time the user finishes entering the songs, and then show a list of songs for the user to type the song name to remove it from the list, then another print showing the final playlist, right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.