How to create a random 5-digit number starting from 1?

0

I have to create a bank account with some requirements, in the middle of them I need to have a constant number of 5 digits started from 00001.

But putting "00001" at the time of display only displays 2, 3 onwards. my solution was to use the number 10000. But I wanted to know how to do it according to the statement.

public abstract class Account implements IBaseRate {

    String name;
    String sSN;
    double balence;
    String accountNumber;
    double rate;
    static int index = 00001;

...

private String setAccountNumber() {

    String lastSSN = sSN.substring(sSN.length()-2, sSN.length());
    int uniqueID = index++;
    int randomNumber = (int) (Math.random()*Math.pow(10, 3));

    return lastSSN + uniqueID + randomNumber;
}
}
    
asked by anonymous 14.02.2018 / 18:14

1 answer

6

The problem here is that you need to format the account number with zeros on the left to fill the number of houses it describes.

One way to do this is by using the String.format , this way

class Main {
    public static void main(String[] args) {
        int num = 1;
        String strNum = String.format("%05d", num);            
        System.out.println(strNum); // Saída: 00001
    }
}

See working on repl.it     

14.02.2018 / 18:19