Add 0 to the left in an ArrayList Integer

1

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

Thank you.

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;



}
    
asked by anonymous 18.10.2018 / 20:36

4 answers

0

You can try this way too, Instead of using everything as "Integer" already start working with "String". But the cat's jump is even in control: 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;
    }

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

    
18.10.2018 / 21:03
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 this with this code:

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

Here's the output:

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

See here working on ideone.

    
18.10.2018 / 20:49
1

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

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;

}
    
18.10.2018 / 20:51
1
  

But for numbers smaller than 10 I'd like to add 0 before   type: 01.02 ... 10.

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

    
18.10.2018 / 20:55