How to create a random number of 5 digits starting from 1?

Asked

Viewed 434 times

4

I have to create a bank account with some requirements, in the middle of them I need to have a 5 digit starting number of the 00001.

But when putting "00001" at the time of displaying only displays the 2, 3 onwards. my solution was to use the number 10000. But I wanted to know how to do 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;
}
}
  • 4

    00001 is a number of one digit formatted with zeros on the left.

1 answer

9


The problem there is that you need to format the account number with zeros on the left to fill in the number of boxes you describe.

One of the ways to do this is by using the method String.format, in 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

  • Thanks! It worked =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.