"Cannot find variable charAt()" error in Java
I am getting the following error while running my simple Java program. This w开发者_如何学Goill lead me to make a bigger module.
Error
C:\Java prog>javac Testt.java
Testt.java:10: cannot find symbol
symbol : variable charAt
location: class java.lang.String
kk=k.charAt[i];
^
The program
class Testt
{
public static void main(String args[])
{
String k="my name is bhola ram";
for(int i=0;i<10;i++)
{
System.out.println(k.charAt[i]);
}
}
}
try this:
System.out.println(k.charAt(i));
the difference is that you're using array subscripts [] rather than the parantheses required for a function call. (thanks Andy)
doc for charAt()
charAt() is a method of the String class.
methods take parameters not indices.
The first ones are given between ( and ).
The second ones are given between [ and ], and are often used for indicating the position in an array.
The mistake is made, I suspect, because in many programming languages a String is a kind of Array of chars that can take parameters.
So MyString[2]
would be valid. This is not the case in Java however, hence the charAt(). method.
精彩评论