Add 0 to the left in an Arraylist <Integer>

Asked

Viewed 66 times

1

Hello, this code returns 6 values between 1 and 60 without repeating and in order. But for numbers smaller than 10 I would like to add 0 before type: 01,02...10. I tried using String.format() but it gives error when compiling.

Thanks.

private static String sortear() {
    Set<Integer> numberset = new HashSet<>();
    Random random = new Random();

    while (numberset.size() < 6) {
        numberset.add((random.nextInt(60) + 1));
    }

    ArrayList<Integer> jogo = new ArrayList<>(numberset);
    Collections.sort(jogo);


    String vetor = Arrays.toString(jogo.toArray());

    return vetor;



}
  • 1

    This part of the code has nothing to do with the error you describe in the question. The part where you give the error you have not posted. Post it so we can see where the error is and how we can help. And show the error too, the more useful information you give the more chances we have to help.

  • It’s what I’m doing for Android Studio, when I add at the end of the code : Return String.format( Format.GAME, vector); The error is this: FATAL EXCEPTION: main java.util.Illegalformatconversionexception: %d can’t format java.util.Arraylist Arguments

4 answers

1

Try this:

import java.util.stream.Collectors;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;

class Teste {
    private static final Random RND = new Random();

    private static String comZero(int x) {
        return (x < 10 ? "0" : "") + x;
    }

    private static String sortear() {
        SortedSet<Integer> numberset = new TreeSet<>();
        while (numberset.size() < 6) {
            numberset.add((RND.nextInt(60) + 1));
        }
        return numberset.stream().map(Teste::comZero).collect(Collectors.toList()).toString();
    }
}

I tested it with this code:

public static void main(String[] args) {
    System.out.println(sortear());
}

Here’s the way out:

[01, 28, 34, 46, 48, 49]

See here working on ideone.

  • Thanks for the return, I tested the Richard Willian before and worked as I wanted, I tested the one that sent me and also worked, mto thanks.

1

You can create a string array just to handle this return and use the string format method, this way:

private static String sortear() {
    Set<Integer> numberset = new HashSet<>();
    Random random = new Random();

    while (numberset.size() < 6) {
        numberset.add((random.nextInt(60) + 1));
    }

    ArrayList<Integer> jogo = new ArrayList<>(numberset);
    Collections.sort(jogo);

    List<String> jogoString = new ArrayList<String>();

    for (Integer num : jogo) {
        //%02d diz que vc quer uma string com tamanho de 2 digitos
        jogoString.add(String.format("%02d", num));
    }


    String vetor = Arrays.toString(jogoString.toArray());

    return vetor;

}
  • I tested it here, but it didn’t work for me, Richard Willian’s solved my question, I appreciate the help!

1

But for numbers smaller than 10 I would like to add 0 before type: 01,02...10.

Only with String.format("%02d", 3); already solves this formatting you need.

  • I tried this, but it gives the following error: FATAL EXCEPTION: main java.util.Illegalformatconversionexception: %d can’t format java.util.Arraylist Arguments

  • Put the part that gives the error.

  • I followed the guidance of Richard Willian, and solved the problem. I appreciate the help.

  • @Antoniosantos, I believe that to make that mistake, he put the Array already converted into String within the method, in place of the 3. String.format("%02d", ArrayList.toString()); So must have given the error.

0


You can try that way too, Instead of using everything as "Integer" already start working with "String". But the cat jump is really in charge: String.format("%02d", NumeroQueVoceQuerFormatar).

private static String sortear() {

        Set<String> numberset = new HashSet<>();

        Random random = new Random();

        while (numberset.size() < 6) {
            String numero = String.format("%02d", (random.nextInt(60) + 1));
            numberset.add(numero);
        }

        ArrayList<String> jogo = new ArrayList<>(numberset);
        Collections.sort(jogo);

        String vetor = Arrays.toString(jogo.toArray());

        return vetor;
    }

That was the result: [08, 21, 25, 33, 34, 48]

  • Thank you very much Richard, it worked exactly as you wanted it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.