Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the do-while loop not run as expected in this Java program?

Tags:

java

do-while

I've been writing a program that will take an array of the names of music files and play them. I succeeded in doing that, but, I wanted to touch up on some things and make it a little nicer. I am trying to make the music play in a random order but not repeat any songs before the entire list has been played. I was almost able to do it but I think there is something wrong with my do-while loop. The program runs as intended for about eight songs but then it stops playing the music and the JVM continues to run. I am using BlueJ as I am still an AP Comp Sci student so I realize that I might not be able to accomplish this task but any help would be greatly appreciated. I have a driver, "MusicDriver" that is a "has-a" relationship with two other classes: "MP3" and "Music".

my MP3 class:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;

public class MP3 {
    String filename;
    Player player; 

    public void stopMP3() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
    public void playMP3(String filename) {
    try {
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        player = new Player(bis);
    }
    catch (Exception e) {
        System.out.println("Problem playing file " + filename);
        System.out.println(e);
    }

    // run in new thread to play in background
    new Thread() {
        public void run() {
            try { player.play(); }
            catch (Exception e) { System.out.println(e); }
        }
    }.start();
}  
}

my Music class:

import java.util.*;

public class Music{
private ArrayList<String> music;

public Music(){music = new ArrayList<String>();}

public int size(){return music.size();}

public void addSong(String song){music.add(song);}

public String getSong(){return music.get(music.size());}

public String getSong(int num){return music.get(num);}

public void removeSong(String song){
    for(int i = 0; i < music.size(); i++){
        if(music.get(i).equals(song)) {music.remove(i); return;}
    }
}

public String toString(){
    String s = "";
    for(int i = 0; i < music.size(); i++){
        s += music.get(i);
    }
    return s;
}
}

my MusicDriver class:

import java.util.*;
import java.io.*;
import javazoom.jl.player.Player;
import java.util.Random;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class MusicDriver{
public static void main(String[] args) throws FileNotFoundException{
    Random r = new Random();
    Scanner s = new Scanner(System.in);
    String line = "";
    int number;

    Music song = new Music();
    song.addSong("1-01-overture.mp3");
    song.addSong("1-03-fortune-teller-2.mp3");
    song.addSong("1-07-prayer.mp3");
    song.addSong("1-08-island-atlas.mp3");
    song.addSong("1-12-warren-report.mp3");
    song.addSong("1-13-avilla-hanya.mp3");
    song.addSong("1-20-war-situation.mp3");
    song.addSong("2-10-fog-of-phantom.mp3");
    song.addSong("2-12-religious-precepts.mp3");
    song.addSong("2-14-box-of-sentiment.mp3");
    song.addSong("3-02-light-everlasting.mp3");
    song.addSong("3-09-viking-spirits.mp3");
    song.addSong("3-12-unsealed.mp3");
    song.addSong("3-16-notice-of-death-reprise-.mp3");
    //14 songs

    ArrayList<Integer> songNums = new ArrayList<Integer>();
    MP3 mp3 = new MP3();
    do{
        if(songNums.size() == song.size()) songNums.clear();

        number = r.nextInt(song.size());
        boolean done = false;
        int counter = 0;
        while(!done){
            for(int i = 0; i < songNums.size(); i++){
                if(number == songNums.get(i).intValue()) {number = r.nextInt(song.size()); counter++;}
            }
            if(counter == 0) done = true;
            else done = false;
        }

        songNums.add(number);
        mp3.playMP3(song.getSong(number));
        System.out.println("Now Playing " + song.getSong(number));
        System.out.println("Enter \"Stop\" to stop playing the song");
        System.out.println("Enter \"n\" to play the next song");
        line = s.nextLine();
        mp3.stopMP3();
    }while(line.equals("n"));
    mp3.stopMP3();
}
}

I have done a lot of research on why my program just stops playing my songs but I haven't been able to find anything. I did, find that BlueJ programs don't open the terminal window (the thing that comes up when you do a "System.out.print()") if you ask for input before you have any output but I don't think that comes into account for this program. I have also made sure that I have input a String "n" when I want to play the next song and for the first couple songs, it works, but after the eighth song, it just stops. I am thoroughly confused.

like image 564
Cal Dilworth Avatar asked Mar 28 '16 12:03

Cal Dilworth


1 Answers

I think the only issue lies in the logic that you are using for shuffling the list.

number = r.nextInt(song.size());
boolean done = false;
int counter = 0;
while(!done){
    for(int i = 0; i < songNums.size(); i++){
        if(number == songNums.get(i).intValue()) {number = r.nextInt(song.size()); counter++;}
    }
    if(counter == 0) done = true;
    else done = false;
}

When the random number generated is already existing in the songNums list, you are generating a new random number. This new random number is not checked with all numbers of songNums list. The following change should solve your problem.

    boolean done = false;
    while(!done){
        number = r.nextInt(song.size());
        if(!songNum.contains(number)) done = true;
    }

Alternatively, you can use Sasha's suggestion in the comments for shuffling the list(Collections.shuffle()).

like image 104
Sendhilkumar Alalasundaram Avatar answered Oct 18 '22 08:10

Sendhilkumar Alalasundaram