lundi 15 février 2021

Converting a string of ten digits to integer or generating a 10 digit long random integer to create an account number in Java

I would like to generate a ten digit account number greater than 1,000,000,000 and less than 9,999,999,999. I have 3 separate attempts that all are almost operational, but after 9 digits I either receive an error or a number that is generated is not an expected outcome.

        for(int i = 0; i < 10; i++) {
            int r = rand.nextInt(10);
            int l = (int)Math.pow(10,i);
            int currentRand = r * l;
            System.out.println("i: "+ i+ ", random int: "+ r+", ten to the pow i: "+ l+ ", currentRand: "+ currentRand);
        }
        int n = rand.nextInt(89999) + 10000;
        int n1 = rand.nextInt(89999) + 10000;
        int n2 = Math.multiplyExact(n*100000);
        System.out.println(n);
        System.out.println(n1);
        System.out.println(n2);
    int accountNum;
        String ofNumber = "";
        for(int i = 0; i < 10;i++) {
            ofNumber += String.valueOf(rand.nextInt(10));
            System.out.println(ofNumber);
        }
        accountNum = Integer.parseInt(ofNumber);



Aucun commentaire:

Enregistrer un commentaire