title: How to shuffle and play an audio file from an array
body: <p>I have some problems with my code, I want to shuffle a file array that contains .mp3 audio files and play one file from it when a button is clicked!! I absolutely have no Idea on how to do this, I need some help, real help!!  Here is my code!..... It runs but it plays all files at once.... I want it to randomize, and play just one out of the files. </p>
  
  <pre><code>import javax.swing.*;
  
  import sun.audio.AudioPlayer;
  import sun.audio.AudioStream;
  
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.lang.reflect.Array;
  import java.util.Random;
  
  public class Quest1 extends JFrame {
  
      String word [] = { "C:/Users/HP/Desktop/eclipse/audio.wav",
              "C:/Users/HP/Desktop/eclipse/baby.wav",
              "C:/Users/HP/Desktop/eclipse/board.wav",
              "C:/Users/HP/Desktop/eclipse/bomb.wav",
              "C:/Users/HP/Desktop/eclipse/gym.wav",
              "C:/Users/HP/Desktop/eclipse/football.wav",
              "C:/Users/HP/Desktop/eclipse/school.wav",
              "C:/Users/HP/Desktop/eclipse/keyboard.wav",
              "C:/Users/HP/Desktop/eclipse/computer.wav",
              "C:/Users/HP/Desktop/eclipse/name.wav"  };
  
  
      JButton click;      
  
      public Quest1 () {
  
          getContentPane().setBackground(Color.DARK_GRAY);
  
          setLayout(new GridBagLayout());
  
          GridBagConstraints g = new GridBagConstraints();
  
  
          g.anchor = GridBagConstraints.CENTER;
          g.gridx = 4;
          g.gridy = 5;
          g.gridwidth = 2;
          g.insets = new Insets (50, 2, 2, 2);
          g.fill = GridBagConstraints.CENTER;
          fill = new JTextField(15);
          add (fill, g);
  
          g.anchor = GridBagConstraints.CENTER;
          g.gridx = 4;
          g.gridy = 8;
          g.gridwidth = 2;
          g.insets = new Insets (30, 2,2,2);
          click = new JButton("Play");
          add(click, g);
  
          click.addActionListener (new ActionListener (){
              public void actionPerformed (ActionEvent x) {
  
                  Random rand = new Random();
                      for (int i = 0; i &lt; word.length;) {
                          int random = rand.nextInt(word.length);
                          String temp = word[i];
                          word [i] = word[random];
                          word[random] = temp;
  
                          InputStream in =null;
                          AudioStream out = null;
                              try {
                                  in = new FileInputStream(temp);
                              } catch (FileNotFoundException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
                              try {
                                  out = new AudioStream(in);
                              } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }       
                              AudioPlayer.player.start(out);
                      }       
                      return;
              }
          });     
      }
  }
  </code></pre>