sto ottenendo un errore in Java durante la compilazione "tipi incompatibili":errore Java: messaggio
UserID.java:36: error: incompatible types
+ generator.nextInt(10);
^
required: String
found: int
Ecco il codice Java:
public class UserID {
private String firstName;
private String userId;
private String password;
public UserID(String first) {
Random generator = new Random();
userId = first.substring(0, 3) +
+ generator.nextInt(1) +
(generator.nextInt(7) + 3) + generator.nextInt(10); //this works
password = generator.nextInt(10) + generator.nextInt(10); //Error is here
}
}
Qual è il motivo di questo errore e come lo aggiusto? Perché non promuove automaticamente l'int a una stringa?
Leggere il messaggio di errore, sembra che stia cercando una stringa ma che abbia trovato un int. hmm ... Mi chiedo se ci sia una funzione che converte un intero in una stringa. – arunkumar
Per inserire un intero in una stringa avete due scelte: 1. 'Integer.toString (yourint)'. e 2. Aggiungi una stringa vuota al tuo intero come questo 'String mystring =" "+ 25'. Se non lo fai, il compilatore ti farà sapere che hai fatto un errore. Giustamente, stai mettendo qualcosa a cui non appartiene. –