Here is the code that gives error:
int frase;
frase = "hola";
And the error pointing to the last of these lines:
error: incompatible types: String can not be converted to int
Here is the code that gives error:
int frase;
frase = "hola";
And the error pointing to the last of these lines:
error: incompatible types: String can not be converted to int
Java is a language in which you have to declare the type that will be stored in each of the variables. In this case you are trying to enter a String into an integer and that is why it gives you the error.
String frase;
frase = "hola";
On the other hand, if you want to store a whole number, you should use the value int
.
int numero;
numero = 5;
You have to bear in mind that when you declare a variable with its type it will not modify its type throughout the execution of the program .
Give them, welcome to the community, your mistake is as follows
The phrase variable is declared as data type int
and this data type is only for whole numbers. To declare phrases (words or texts) you must occupy String
String frases;
frases = "hola";
type error
String frase;
frase = "hola";
the strings are string integers are int
It gives you an error since you are initializing a variable of type int
(integer) when what you need is a variable of type String
(text type)
Option in 2 lines (Initialization and Assignment):
String frase;
frase = "hola";
Option in one line (Initialization + Assignment):
String frase = "hola";
I hope I have helped you:)
int
is only used for integers (numbers).
You should declare it as String
(string).
String frase;
frase = "hola";
Edited:
It has already been explained in this user response @ Error_404 , but to give it a little more depth:
int
is a data type that includes integer numeric values (you can not save a numerical value with decimals in an int). The variables that you save with ""
, are not that kind of values, but are text strings, which are typed previously with String
.
When assigning a int
(which only receives numbers) to a value String
, that error occurs. Remember that in Java, the variables are typed - you must indicate the type to declare them.