Error with words in Java - "error: incompatible types: String can not be converted to int"

1

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

    
asked by Danlos 16.11.2016 в 14:22
source

6 answers

9

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 .

    
answered by 16.11.2016 в 14:28
2

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";
    
answered by 16.11.2016 в 14:24
2

type error

String frase;
frase = "hola";

the strings are string integers are int

    
answered by 16.11.2016 в 14:24
2

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:)

    
answered by 16.11.2016 в 15:20
1

int is only used for integers (numbers).
You should declare it as String (string).

String frase;
frase = "hola";
    
answered by 16.11.2016 в 14:25
1

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.

    
answered by 19.11.2016 в 01:13