简体   繁体   中英

in Java, how to create n number of Strings with certain properties

What I would like to do is produce n number of Strings and each string should have 2 char and 4 numbers randomly shuffled, for ex: 2w3e45 34rt56 qw5498 0126fd w5487t

and so on, I have the following, it is working, any enhancements could be applied? Also, can we have them as unique strings?

import java.util.Random;

public class RandomNumAndChar {

public static void main(String[] args) {

    for (int j = 0; j < 10; j++) {
        Random r = new Random();
        Integer[] rn = new Integer[4];
        String numbers = new String();
        for (int i = 0; i < rn.length; i++) {
            rn[i] = r.nextInt(10);
            numbers = numbers + rn[i].toString();
        }
        char c = (char) (r.nextInt(26) + 'a');
        char c2 = (char) (r.nextInt(26) + 'a');
        String chars = "" + c + c2;
        String fin = numbers.concat(chars);
        System.out.println(RandomNumAndChar.shuffle(fin));


    }


}

public static String shuffle(String s) {
    char[] characters = s.toCharArray();
    for (int i = 0; i < characters.length; i++) {
        int randomIndex = (int)(Math.random() * characters.length);
        char temp = characters[i];
        characters[i] = characters[randomIndex];
        characters[randomIndex] = temp;
    }
    return new String(characters);
}

}

As stated in the comments this is more suitable for CodeReview Stack Exchange . However here are some guidelines I'll use to perform the task:

  • Use a 6 Character array to store the code. Beware I said Character , not char . This is because Java is more flexible when dealing with wrappper classes.
  • Use Collections#shuffle() to shuffle your array. Here is where Character comes in hand, since we need to convert the array in a List with Arrays#asList() .
  • Use a method to generate the whole code, not only for shuffling.

Using this simple guidelines you get something like:

class Test {

    static Random r = new Random();

    public static void main(String[] a) {

        for (int i = 0; i < 10; i++) {
            System.out.println(getNewCode());
        }
    }

    private static String getNewCode() {
        Character[] array = new Character[6];

        int i = 0;
        while(i < 4) array[i++] = (char) ('0' + r.nextInt(10));

        array[4] = (char) ('a' + r.nextInt(26));
        array[5] = (char) ('a' + r.nextInt(26));

        List<Character> l = Arrays.asList(array);

        Collections.shuffle(l);

        StringBuilder sb = new StringBuilder();

        for (char s : l)
            sb.append(s);

        return sb.toString();
    }
}

Sample output:

e1v456
8s632b
t7810w
n9801y
76q14c
p2a881
27m7k1
1081st
m19u36
t03h77

More conviniently you could start directly using a List instead of an array:

class Test {

    static Random r = new Random();

    public static void main(String[] a) {

        for (int i = 0; i < 10; i++) {
            System.out.println(getNewCode());
        }
    }

    private static String getNewCode() {
        List<Character> l = new ArrayList<Character>();

        int i = 0;
        for(int i = 0; i<4; i++)
            l.add((char) ('0' + r.nextInt(10)));

        l.add((char) ('a' + r.nextInt(26)));
        l.add((char) ('a' + r.nextInt(26)));

        Collections.shuffle(l);

        StringBuilder sb = new StringBuilder();

        for (char s : l)
            sb.append(s);

        return sb.toString();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM