1

The question is pretty common, but, because I'm a Java noob and I haven't much practice with it, I couldn't apply the answers I found in other topic.

So here's my case:

I want to have an ArrayList containing the file's name of a specified folder, and with this code I can have it:

File dir = new File(" ++++++ Insert Path Here ++++++");
        List<String> list = Arrays.asList(dir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".mp3");
            }
        }));

String[] stockArr = new String[list.size()];
        stockArr = list.toArray(stockArr);

After that, I want to name some JButtons with random strings contained in stockArr, ok, pretty easy to do:

        JButton bottone1 = new JButton();
        JButton bottone2 = new JButton();
        JButton bottone3 = new JButton();

        Random r = new Random();

        bottone1.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone2.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone3.setText(stockArr[r.nextInt(stockArr.length)]);

Now my problem: How I can avoid to have duplicate random String in JButton's Text?

P.S.: I have to say that sometimes, searching for answer, I couldn't understand someone's suggestion beacause they write just a part of the code, or they assume that other person have the knowledge to understand at full what they write. So I'm asking if you please can be more specific at possible, step after step, just like tutorial, that will be very appreciate :)

The whole code:

import java.awt.Container;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class BottoniRandom {

    public static void main(String[] args) {

        JFrame finestra = new JFrame("ESEMPIO");
        finestra.setBounds(100, 500, 300, 200);

        Container contenuto = finestra.getContentPane();
        contenuto.setLayout(new BoxLayout(contenuto, BoxLayout.Y_AXIS));

        JButton bottone1 = new JButton();
        JButton bottone2 = new JButton();
        JButton bottone3 = new JButton();

        File dir = new File(" ++++++ Insert Path Here ++++++");
        List<String> list = Arrays.asList(dir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".mp3");
            }
        }));

        String[] stockArr = new String[list.size()];
        stockArr = list.toArray(stockArr);
        System.out.println(stockArr.length);
        Random r = new Random();

        bottone1.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone2.setText(stockArr[r.nextInt(stockArr.length)]);
        bottone3.setText(stockArr[r.nextInt(stockArr.length)]);

        contenuto.add(bottone1);
        contenuto.add(bottone2);
        contenuto.add(bottone3);

        finestra.setVisible(true);
        finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
BROKENCODE
  • 85
  • 1
  • 10
  • 1
    Shuffle the array, and assign the 0th element to button1, the 1st element to button2 etc. – Andy Turner Aug 31 '17 at 19:00
  • I tried: bottone1.setText(stockArr.shuffle); but it's not correct – BROKENCODE Aug 31 '17 at 19:03
  • Well, that's not the way to shuffle an array. The easiest thing to do actually is just shuffle the list, and set the elements from there: `Collections.shuffle(list); b1.setText(list.get(0)); b2.setText(list.get(1));` etc. No array is necessary. – Andy Turner Aug 31 '17 at 19:08
  • If I do as you suggested, I get an error: `Exception in thread "main" java.lang.NullPointerException at java.util.Objects.requireNonNull(Unknown Source) at java.util.Arrays$ArrayList.(Unknown Source) at java.util.Arrays.asList(Unknown Source) at BottoniRandom.main(BottoniRandom.java:21)` I think I have to do something like: `String[] shuffledArr = Collections.shuffle(list);` – BROKENCODE Aug 31 '17 at 19:18
  • Hi BC. If you have a response to the suggested duplicate, please add it to the comments - it is just confusing prepended to the question. Our experience is that questions so edited will be stuck like that, and so the first thing readers see if they read the question is meta-commentary about voting, which is rather confusing. – halfer Aug 31 '17 at 22:11
  • Hi halfer, thanks for suggest it. I edited in the main post because the message says "If this question is different, please edit it to explain how it is different" I think it means do it there. The question is different, because I asked for a more specific solution. I'm all new to program language and I find difficult read a semi-answer somewhere and apply it to my case. I searched before posting the answer, I've seen the one suggested, but I wasn't able to apply it to my problem. That's all. – BROKENCODE Sep 01 '17 at 10:28
  • Ok, let try this. What if I want the most pseudo-random without duplicate name possible? In the case suggested, in the end, a fixed Array is created and then you have to use int value as index: `bottone1.setText(stockArr[1]);` If I try to make the index value random, the problem will be the same: some indexes may be repeated. Again I'll have to shuffle the list, make an array from it, and then assign the first 3 indexes everytime ?(`bottone1.setText(stockArr[stockArr[0]]); bottone1.setText(stockArr[stockArr[1]]); bottone1.setText(stockArr[stockArr[2]]);`)? Is that correct? – BROKENCODE Sep 02 '17 at 15:38

0 Answers0