Results 1 to 8 of 8
Thread: charAt problem... Please help
- 01-20-2011, 01:45 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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.
When I run it Eclipse displays this in the Debug tab:Java 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; } }
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:
But as soon as I add n++ to the end of the loop I get the same error.Java 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; }
Please help.
Matija
- 01-20-2011, 01:58 AM #2
- 01-20-2011, 02:55 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
try this on the command line. It should not have any errors. the problem lies with your IDE. Learn your Java programming without the IDE.
Java Code:import java.util.*; public class Capitalize { public static void main(String[] args){ System.out.print("Insert a string to capitalize: "); String str= new Scanner(System.in).nextLine(); System.out.println(capitalize(str)); } private static 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; } }
- 01-20-2011, 01:20 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Thanks for all the help.
I haven't defined any custom classes, and yes the problem lies in the IDE.
I suspected this since I had similar problems with other methods before.
The above given code works.
As I said, I'm really fresh and am going through the book and some courses.
At this point I'm just following along with them.
Thanks again for all your troubles.
Cheers
Matija
- 01-21-2011, 12:23 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
If I understand you correctly, there's not really a problem with your original code, but you cannot "step into" the String.charAt() method in the debugger because you don't have source code for it.
-Gary-
- 01-21-2011, 01:15 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Well, the String.charAt() method works fine as long as I pass an unchanging variable to it.
As I wrote in the first post, if i declare an integer n to be 0, 1, or whatever and
pass it through String.charAt() it works fine. It doesn't give me a desired result, but it works. But as soon as I do something to that n to change it at the end of the loop and run it, it sends me to debugger which states:
String.charAt(int) line: not available
Thanks to JavaHater I know how to avoid the problem but the actual cause of it escapes me...
I had similar problems with a couple of other methods, though not this crucial. This one breaks the program completely while others wouldn't get executed. Program would continue like I never called them.
Matija
- 01-21-2011, 01:18 AM #7
- 01-21-2011, 01:20 AM #8
Similar Threads
-
Space for charAt()?
By Tussmann in forum New To JavaReplies: 5Last Post: 11-02-2010, 06:57 PM -
Help with charAt()
By HackerOfDoom in forum New To JavaReplies: 7Last Post: 03-21-2010, 05:27 PM -
using String.charAt() for semicolons
By porchrat in forum New To JavaReplies: 5Last Post: 01-11-2010, 01:40 PM -
Palindrom - method charAt()
By user in forum New To JavaReplies: 10Last Post: 11-16-2008, 04:37 AM -
Help With Input.charAt(LastIndex);
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks