Bilal Haider
Bilal Haider

Reputation: 99

How to generate a random string starting with a letter in Java

Been a while since I posted a question! Basically I'm starting a new project in University with Java! Basically what is needed of me is to generate a object to store items inside them. This includes product codes, item names, prices, etc. Basically for the product code I am in charge of deciding what length to have it BUT it must start with a Letter not a number. I was thinking of 6 characters. But how do I tell Java to keep the first character a letter and not a number?

Much Appreciated :D

Upvotes: 1

Views: 3854

Answers (5)

CodeAddict
CodeAddict

Reputation: 194

public static void main(String[] args) {
    //random arrays
    String[] randomLetters = {"a", "b", "c", "d", "e", "f", "g", "h", etc...}
    String[] randomNumbers = {"08724 ","13876 ","29238 ","37534 ","40182 ","57532 ","69273 ", etc...};

    //chooses random element from the letters
    Random r = new Random();
    int randl = r.nextInt(8);
    String randomLetter = randomLetters[randl];

    //chooses random element from the numbers
    Random a = new Random();
    int randn = a.nextInt(7);
    String randomNumber = randomNumbers[randn];

    //turns into one string and presents it
    String str1 = randomLetter + randomNumber;
            System.out.print(str1);
    }
    }

I don't know if this is what you wanted exactly -- it's sort of a one time thing. I was just trying to do it in simple form.

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

public String getProductCode(){
    Random random = new Random();
    int first = random.nextInt(26) + 65;  //Get random ASCII code in letter range
    char firstChar = new Character((char) first);  //Convert to char
    int suffix = 10000 + random.nextInt(89999);  //Get 5 digit suffix
    return Character.toString(firstChar) + String.valueOf(suffix);
}

Upvotes: 2

Diego Basch
Diego Basch

Reputation: 13059

Try something like this, it's hacky but you get the point:

import java.util.Random;

public class foo {

  public static void main(String args[]) {
    String chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random r = new Random();
    int limit = 5;
    StringBuffer buf = new StringBuffer();

    buf.append(chars.charAt(r.nextInt(26)));
    for (int i = 0; i < limit ; i++) {
      buf.append(chars.charAt(r.nextInt(chars.length())));
    }
    System.out.println(buf);

  }
}

Upvotes: 1

Sean F
Sean F

Reputation: 2390

store them seperatly in an object and return the combination in a different getter

public class Product {

    protected char id;
    protected int code;

    public String getFullcode() {
        return id + code.toString();
    }

    //regular getters and setters
}

Upvotes: 1

Nile
Nile

Reputation: 25

Just make two different random variables one that selects a random letter and the another that picks any random character. Then add them in the obvious way. Hope this helps

Upvotes: 0

Related Questions