Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

string.atChar(0) crashes

I have an array koal of strings with "00" at koal[0] but this attempt to get 1st
element from first string crashes:

tv6.setText(koal[0].charAt(0));

On the other hand:
tv6.setText(koal[0]) prints "00".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why? Or, how do I get ith element from koal[0] ?

>Solution :

charAt() returns a char, which is a numeric type that is then coerced to an int, meaning that you’re accidentally calling the overload of setText() that expects to be passed an R.string resource id.

If you want to use the overload of setText() that accepts a string, you’ll have to manually convert that char to a string somehow. One very easy way is to use string concatenation:

tv6.setText("" + koal[0].charAt(0));

Of course, there are many ways to convert char to String; this is just the easiest.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading