Using String

import java.util.Scanner;
public class String {
      public static void main(String[] args) {
          Scanner sc = new Scanner (;

          String cadena;

          System.out.println("Introcuce un nombre");
          cadena = sc.nextLine();

          System.out.println("Buenos dias "+cadena);



Why does the program tell me the following?


String can not be converted to string

asked by Carlos 08.10.2016 в 10:30

2 answers


You have two problems with the code one is the input parameter in your main function, since you are using your String class and not the one defined in java by default, this same case is repeated with your string variable, which deals with to solve with the class that you have defined.

In order not to modify your code much, you should solve the program in the following way:

import java.util.Scanner;

public class String {

    public static void main(java.lang.String[] args) {
        Scanner sc = new Scanner(;
        java.lang.String cadena;

        System.out.println("Introduce un nombre");
        cadena = sc.nextLine();

        System.out.println("Buenos días " + cadena);

PS: Do not forget to close your scanner object.

answered by 09.10.2016 в 02:05

Your class is called the same as the data type String , so the compiler thinks that when you use String within the main you are referring to the class, but not the data type.

Do not put names of reserved words or classes existing in the API to your classes.

answered by 08.10.2016 в 12:05