package com.java.learn;
import java.util.Random;
public class RandomDemo {
String str = "0123456789";
public static void main(String[] args) {
int length = 10;
System.out.println("otp is" + getOtp(length));//Must explicitly convert the char[] to a String
}
public static char[] getOtp(int len) {
// TODO Auto-generated method stub
String str = "0123456789";
Random random = new Random();
char[] otp = new char[len];
for (int i = 0; i < len; i++) {
otp[i] = str.charAt(random.nextInt(str.length()));
}
return otp;
}
}
While printing the otp why it's giving me warning of that i need to convert char[] to String explicitly.But when i print it without "otp is" it runs successfully.
Aucun commentaire:
Enregistrer un commentaire