lundi 18 octobre 2021

Trying to generate random text in Java - "Type mismatch: cannot convert from long to int"

I'm trying to generate 100 characters of random text in Java. Here is my code:

import java.lang.Math;
public class RandomTextGenerator {
    public static void main(String[] args) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        for (int i = 0; i < 100; i++) {
        int A = Math.round(62*Math.random());
        int B = A + 1;
    System.out.println(characters.substring(A,B));
    }
}

I get the error "Type mismatch: cannot convert from long to int" but I don't understand this- I clearly declared A and B as ints, not longs, and I'm expecting my code to give me a random integer from 0 to 61 so it shouldn't automatically be a long because 0 and 61 are well within the range of integers.

I am fairly new to Java so apologies if I'm doing something obviously wrong.




Aucun commentaire:

Enregistrer un commentaire