Issue
I want to put the value "restante" in my string array with method push. But an error display:
incompatible types: int cannot be converted to String
My JAVA file
import java.util.Scanner;
public class NumRestantes {
public static void main(String[] args) {
Pilha p1 = new Pilha(10);
Scanner ler = new Scanner(System.in);
int valor;
int restante;
valor = ler.nextInt();
restante = valor%16;
try {
if (restante == 0) {
p1.push(new Numeros(restante, 0));
} else {
while(restante >= 1) {
restante = restante%16;
p1.push(new Numeros(restante, 0));
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
I don't know much about methods, so I'd want some help to push values from variable "restante" to my array string.
Solution
You should convert the int
to a String
, then add it. One way to do this is to use Integer.toString(int)
. This uses the Integer
class to convert an int
into a string containing the int
.
Answered By - Hawkeye5450