Reputation:
I'm taking a Java class for college, and was working on some given tasks. This is the code I wrote for it.
public class String
{
public static void main(String[] args)
{
String city = "San Francisco";
int stringLength = city.length();
char oneChar = city.charAt(0);
String upperCity = city.toUpperCase();
String lowerCity = city.toLowerCase();
System.out.println(city);
System.out.println(stringLength);
System.out.println(oneChar);
System.out.println(upperCity);
System.out.println();
}
}
which yielded these results
C:\Users\sam\Documents\Java>javac String.java
String.java:8: error: incompatible types: java.lang.String cannot be
converted to String
String city = "San Franciso";
^
String.java:9: error: cannot find symbol
int stringLength = city.length();
^
symbol: method length()
location: variable city of type String
String.java:10: error: cannot find symbol
char oneChar = city.charAt(0);
^
symbol: method charAt(int)
location: variable city of type String
String.java:11: error: cannot find symbol
String upperCity = city.toUpperCase();
^
symbol: method toUpperCase()
location: variable city of type String
String.java:12: error: cannot find symbol
String lowerCity = city.toLowerCase();
^
symbol: method toLowerCase()
location: variable city of type String
5 errors
I've tried searching for an answer but I didn't really find anything that helps. Any help is appreciated, thanks.
Upvotes: 0
Views: 25658
Reputation: 1
String is a reserved key word in Java, Please refer to the reserved keywords in java Java Reserved Keywords.
Renaming the class to something other than these keywords would be the solution to your problem. Or you can follow the import statement solution above.
BTW, Please use a good IDE to develop java programs, it seems that your IDE is not showing or linting basic rules.
Upvotes: -2
Reputation: 1
write one statement java.lang.String.*; import java.lang.String; your problem will be solved
Upvotes: 0
Reputation: 969
It is conflict between system class java.lang.String and your class named String. Rename your class String to say MyString, i.e. replace line:
public class String
with
public class MyString
And rename file String.java containing this class to MyString.java.
Upvotes: 4
Reputation: 12450
Since your class is named String
, unqualified type reference in String city
is taken as reference to your own class.
Either rename the class to some other name, or you'll have to write java.lang.String
wherever you reference the "built-in" Java String
class.
Upvotes: 6