-19

I want to generate a 6 character random string on every compilation of a program in Java.

For example: AXFGCD, GDGXSD, PLRSFX, GLTSDL

public class generate{
    public static void main(String args[]){
        generate();
    }

    string word = "";
    string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    void generate(int x){
        if (x==7) return;

        int time = System.currentTimeMillis() % 33; //to generate time in mili second
        word += characters.charAt(time);
        System.out.println(time);
        return generate(++x);
    }
}

output is

A,AX,AXF,AXFC,AXFCG,AXFCGA

but I want direct 6 digit word

Michael
  • 41,989
  • 11
  • 82
  • 128
user8610600
  • 73
  • 1
  • 1
  • 4

2 Answers2

2

This was written in Notepad++ as I currently dont have any IDE available, I hope this compiles and if it does it should work :) If you want to use uppercase characters simply edit the for loop!

public class generate{

    private static char[] characters;
    private static Random random = new Random();

    public static void main(String args[]){
        characters = new char[26];
        int index = 0;
        for (char c = 'a'; c <= 'z'; c++) {
            characters[index++] = c;
        }
        System.out.println(generate());
    }

    static String generate() {
        String word = "";
        for(int i = 0 ; i < 7 ; i++) {
            word += Character.toString(characters[random.nextInt(characters.length)]);
        }
        return word;
    }
}

Edit: using your code!

public class generate{
    public static void main(String args[]){
        generate();
    }

    string word = "";
    string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    void generate(int x){
        if (x==7) {
            System.out.println(word);
            return;
        }

        int time = System.currentTimeMillis() % 33; //to generate time in mili second
        word += characters.charAt(time);
        return generate(++x);
    }
}
Dinh
  • 759
  • 5
  • 16
1

Your method is achieving your purpose. Your problem is that you are "logging" your steps by printing them in your method (Also, there are errors like string, calling your generate funciton incorrectly, but since you gave us the output I guess you fixed it at some point).

If you just do this:

public class generate{
    public static void main(String args[]){
        generate(0);
        System.out.println(word); // Here word is complete

    }

    String word = "";
    String Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    void generate(int x){
        if (x==7) return;

        int time = System.currentTimeMillis() % 33; //to generate time in mili second
        word += characters.charAt(time);
        return generate(++x);
    }
}

it will work. Alternatively, to print your word in your function, you could've use an iterative approach:

void generate(){
    for (int i = 6; i --> 0;) {
        int time = System.currentTimeMillis() % 33; //to generate time in mili second
        word += characters.charAt(time);
    }
    System.out.println(word);
}
Turtle
  • 1,626
  • 16
  • 26