1

I am getting the following error while running my simple Java program. This will 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]);
        }
     }
}

2 Answers 2

8

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

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

3 Comments

+1: I also suggest you try an IDE as it should auto-complete this for you.
@Gautum, to be explicit, the difference is that you're using array subscripts [] rather than the parantheses required for a function call.
@Andy ill add that to the answer
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.