vendredi 4 juin 2021

Java: Method nextLong in class Random cannot be applied to given types [duplicate]

I am working on a random number generator that generates random positive number of a given length. While working on it, I decided to update the function to include long so that it could work with more digits. However, the updated function produces error: method nextLong in class Random cannot be applied to given types; return m + new Random().nextLong(9 * m);

Can anyone help me fix the error message so that I can generate 15 digits numbers? Other than that if you see anything else in my code that needs fixed please let me know. Any help would be appreciated.

import java.util.Random;


public class NumberGenerator {
    public static void main(String[] args) {
        System.out.println("Welcome to Number Generator!");
        System.out.println("Generating 5 Digits:\t" + generateRandom(5));
        System.out.println("Generating 10 Digits:\t" + generateRandom(10));
        System.out.println("Generating 15 Digits:\t" + generateRandomLong(15));

    }

    public static int generateRandom(int n) {
        int m = (int) Math.pow(10, n - 1);
        return m + new Random().nextInt(9 * m);
    }

    public static long generateRandomLong(long n) {
        long m = (long) Math.pow(10, n - 1);
        return m + new Random().nextLong(9 * m);
    }
}



Aucun commentaire:

Enregistrer un commentaire