-2

My program contains this line:

char A= K.charAt(i);

But, when I executed this, I'm getting an error saying:

error: cannot find symbol
      char A= K.charAt(i); 
               ^
symbol:   method charAt(int)
location: variable K of type String[]

What wrong did I do?

Ivar
  • 6,138
  • 12
  • 49
  • 61
Keerthi Nandigam
  • 71
  • 1
  • 2
  • 5
  • 4
    K is a `String[]` (string array), while `chatAt(int)` is a method belonging to `String`. You can get a string from the array by using `K[index]`. – MC Emperor Aug 13 '18 at 11:01
  • Add your entire program. Maybe you are trying to access undeclared variable. – Sumesh TG Aug 13 '18 at 11:01
  • Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – MC Emperor Aug 13 '18 at 11:03
  • Please read about a [mcve] and edit the question accordingly – OneCricketeer Aug 13 '18 at 11:04
  • 2
    Oh and PS: it's always good to stick to the Java Naming Conventions. Variables always start with lowercase. So `A` and `K` should be `a` and `k` respectively. – MC Emperor Aug 13 '18 at 11:08

1 Answers1

2

Form the error message, it says variable K is an array of type String. So to get the value from an array use this syntax, K[i]. Once you get a String from the array, you can use sampleString.charAt(i) to get a char at index i.

Example:

String[] K = {"abc","def","ghi"};
String sampleString = K[1]; // sampleString -> def
char firstChar = sampleString.charAt(0) // firstChar -> d
mc20
  • 1,145
  • 1
  • 10
  • 26