How to generate a random alpha-numeric string

前端 未结 30 2523
忘掉有多难
忘掉有多难 2020-11-21 05:38

I\'ve been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifie

30条回答
  •  清酒与你
    2020-11-21 05:59

    import java.util.*;
    import javax.swing.*;
    
    public class alphanumeric {
        public static void main(String args[]) {
            String nval, lenval;
            int n, len;
    
            nval = JOptionPane.showInputDialog("Enter number of codes you require: ");
            n = Integer.parseInt(nval);
    
            lenval = JOptionPane.showInputDialog("Enter code length you require: ");
            len = Integer.parseInt(lenval);
    
            find(n, len);
        }
    
        public static void find(int n, int length) {
            String str1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            StringBuilder sb = new StringBuilder(length);
            Random r = new Random();
    
            System.out.println("\n\t Unique codes are \n\n");
            for(int i=0; i

提交回复
热议问题