charAt problem... Please help
Hello everyone.
I'm fresh to both programming and Java, so please be patient with me.
I'm reading " The Art and Science of Java" and going through exercises,
currently on chapter 9 "Strings and Characters".
I am having a problem using charAt method.
The exercise I'm at requires a construction of a method that will capitalize a given string regardless of the case the input is. So given the string "BOOLEAN"
or string "boolean" would each return the result "Boolean".
This is a code I have so far.
Code:
import acm.program.*;
public class Capitalize extends ConsoleProgram{
public void run(){
String str= readLine("Insert a string to capitalize: ");
println(capitalize(str));
}
private String capitalize(String word){
String result = "";
for (int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
if(i==0){
ch = Character.toUpperCase(ch);
}else{
ch = Character.toLowerCase(ch);
}
result+=ch;
}
return result;
}
}
When I run it Eclipse displays this in the Debug tab:
String.charAt(int) line: not available
Is my implementation wrong?
This happens every time I include charAt method in a loop and pass a changing variable to it.
I've tried this and it worked:
Code:
for (int i = 0; i < word.length(); i++){
[COLOR="Red"]n=0;[/COLOR]
char ch = word.charAt([COLOR="Red"]n[/COLOR]);
if(i==0){
ch = Character.toUpperCase(ch);
}else{
ch = Character.toLowerCase(ch);
}
result+=ch;
}
But as soon as I add n++ to the end of the loop I get the same error.
Please help.
Matija