Home > Back-end >  How can I create random strings with uppercase and lowercase in Java?
How can I create random strings with uppercase and lowercase in Java?

Time:04-10

So, I found this function on the Internet and I would like to get some guidance on making strings use uppercase letters alongside lowercase ones, the code:

import java.util.Random;

public class randomstring {
    public void rand()
    {
        int leftLimit = 97; // letter 'a'
        int rightLimit = 122; // letter 'z'
        int targetStringLength = 5;
        Random random = new Random();
        StringBuilder buffer = new StringBuilder(targetStringLength);
        for (int i = 0; i < targetStringLength; i  ) {
            int randomLimitedInt = leftLimit   (int) 
              (random.nextFloat() * (rightLimit - leftLimit   1));
            buffer.append((char) randomLimitedInt);
        }
        String generatedString = buffer.toString();

        System.out.println(generatedString);
        
        
    }
    public static void main(String[] args) {
        randomstring random = new randomstring();
        random.rand();
    }
}

CodePudding user response:

The issue with using ascii indexes, is that it's hard to use separate ranges, lower is [97;122] while upper is [65;90], better use indexing in a given list of chars

static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static final String lower = upper.toLowerCase();
static final String symbols = upper   lower;

public static void rand() {
    int rightLimit = symbols.length();
    int targetStringLength = 50;
    Random random = new Random();
    StringBuilder buffer = new StringBuilder(targetStringLength);

    for (int i = 0; i < targetStringLength; i  ) {
        int randomLimitedInt = random.nextInt(rightLimit);
        buffer.append(symbols.charAt(randomLimitedInt));
    }

    String generatedString = buffer.toString();
    System.out.println(generatedString);
}

CodePudding user response:

Slightly different approach, use random nextBoolean() to check whether the generated char should be uppercase.

import java.util.Random;

public class randomstring {
    public void rand() {
        int leftLimit = 97; // letter 'a'
        int targetStringLength = 5;
        Random random = new Random();
        StringBuilder buffer = new StringBuilder(targetStringLength);
        for (int i = 0; i < targetStringLength; i  ) {
            char c =(char) (leftLimit   random.nextInt(26));
            boolean shouldUseUppercase = random.nextBoolean();
            char character = shouldUseUppercase ? Character.toUpperCase(c) : c;
            buffer.append(character);
        }
        String generatedString = buffer.toString();
        System.out.println(generatedString);
    }

    public static void main(String[] args) {
        randomstring random = new randomstring();
        random.rand();
    }
}

CodePudding user response:

import java.util.Random;

public class RandomString {

    public static void main(String[] args) {
        
        StringBuilder randomString = new StringBuilder();
                
        new Random().ints(65, 122)
                    .filter(i->!(i>90&&i<97))
                    .boxed()
                    .limit(12)
                    .map(c->Character.toChars(c))
                    .forEach(c->randomString.append(c));
                
        System.out.println(randomString);
        
    }
    
}

change the value in limit to adjust the size of random String

  • Related