1
import java.util.*;
import java.lang.*;

public class String{ 

    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a String: ");
        String s=in.nextLine();
    }    
}

Here's my code. Whenever i compile this java code i get an error as:

Incompatible types:java.util.String cannot be converted to String

String s=in.nextLine();
                ^  

can any one tell me what should i do to get over it.

4
  • At which line?? Commented Nov 6, 2016 at 11:49
  • 1
    Do not name your class String. Commented Nov 6, 2016 at 11:50
  • 1
    Don't define a class named String. String is already a standard class used basically everywhere. You don't want such a name clash. And when you post an error, post the real one. There is no such thing as java.util.String. Unless you also chose to create your classes in the standard java.util package. Don't do that either. Commented Nov 6, 2016 at 11:50
  • thank you i just didn't noticed it Commented Nov 6, 2016 at 11:52

3 Answers 3

4

You should not use names for your classes that are already used in JRE in package java.lang.*

Sign up to request clarification or add additional context in comments.

Comments

1

It happens because your class is named the same like the Java's String from java.lang.*, that is used basically everywhere.

Rename you class or use of prefixes like com.app.myclass.String.

Comments

0

Using Java packages avoids class name collisions, so you need to use full class name prefixing with the package name (like option1 below), whenever there is a conflict between multiple classes. So to resolve your issue, you can pick one of the following:

(1) java.util.String s=in.nextLine();

(2) Change your class name MyString

You look here on how to avoid class name conflicts.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.