简体   繁体   中英

Printing a list of values from array

I am trying to print the song name and lengths of songs each time the loop finishes. How do I do this? Report+= songTitles[numSongs] + songLengths[numSongs] ?

Then, I need to do a linear search to remove songs from the playlist. Will I need to use the same report string to let the user see all songs? I just need help with that. Thank you.

import javax.swing.JOptionPane;

public class asdf_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++;
    }   
  }
}

I have a few suggestions to make this easier for you.

Firstly, you should create a class to capture you song information rather than having two separate arrays. This will make life much easier for you in the long term (and is better OO practice). You can then create a toString method as part of that class to format song information:

class Song {
    private final String title;
    private final int length;

    public String toString() {
        return title + ":" + length;
    }
}

Your song arrays then become simpler:

private Song[] songs = new Song[MAX_SONGS];

Printing the entire list can be done in a number of ways. Before Java 8 it would generally look something like:

for (Song song: songs)
    System.out.println(song);

Since Java 8 was released this can be simplified to:

Arrays.stream(songs).forEach(System.out::println);

Removing items from an array is not as easy as removing them from a collection. But it's still not too hard:

Song[] copy = new Song[MAX_SONGS];
int copiedSongs = 0;
for (Song song: songs)
    if (/* condition for copying */)
        copy[copiedSongs++] = song;
songs = copy;
numSongs = copiedSongs;

Again, using Java 8 this has become much simpler:

songs = Arrays.stream(songs).filter(/*condition*/).toArray();
numSongs = songs.length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM