Results 1 to 8 of 8
  1. #1
    Matija is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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.

    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;
    	}
    
    }
    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:

    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;
    			
    		}
    But as soon as I add n++ to the end of the loop I get the same error.
    Please help.
    Matija

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Matija View Post
    But as soon as I add n++ to the end of the loop I get the same error.
    This makes no sense whatsoever.

    Your code is fine as far as I can see. The only thing I can think of is that you have created your own String class and it is trying to find the charAt method in this class rather than the Java API String class.

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    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;
        }
    }

  4. #4
    Matija is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  6. #6
    Matija is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by gcalvin View Post
    because you don't have source code for it.
    Yes you do, src.zip and any decent IDE should be able to show it.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Which IDE are you using?

    I have never heard of any such behaviour. As I said your code is fine and can only assume that it is some hinky behaviour of the IDE.

Similar Threads

  1. Space for charAt()?
    By Tussmann in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 07:57 PM
  2. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 06:27 PM
  3. using String.charAt() for semicolons
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 01-11-2010, 02:40 PM
  4. Palindrom - method charAt()
    By user in forum New To Java
    Replies: 10
    Last Post: 11-16-2008, 05:37 AM
  5. Help With Input.charAt(LastIndex);
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •