Results 1 to 7 of 7
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default Why won't charAt() work?

    I need to total the number of lines in a string. In my finite programming wisdom I know that charAt is a valid String method. However when I use it in the code below Eclipse keeps telling me it is not a valid method for my class. What gives?!?!?

    Java Code:
    public void addClassToSchedule(int index) throws TimeConflictException,
    			NameConflictException {
    		String list = allClasses();
    		int total = CourseList.howManyItems(all);
    		int linenumber = 0;
    		String c
    		for(int t = 0; t < list.length(); t++){
    				String c = list.charAt(t);
    				linenumber++;
    			}
    		}

  2. #2
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Never Mind, just used a StringTokenizer instead.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    Eclipse keeps telling me it is not a valid method for my class
    Was there more to the message?
    The problem I see is with what charAt() returns.
    Java Code:
    TestCode5.java:379: incompatible types
    found   : char
    required: java.lang.String
           String c = "asdf".charAt(1);
                                   ^
    1 error
    vs
    
      char c = "asdf".charAt(1);

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

    Default

    In future post the full and exact error message not your paraphrased interpretation.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, you should avoid using string tokenizer and stick to split instead. StringTokenizer is a legacy class only still around for old code.

  6. #6
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Look at what the charAt() returns. A String and a char are not compatible types. You need to convert the char to a String.

    Hint: Check the Character class.

  7. #7
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by MetalR0 View Post
    Never Mind, just used a StringTokenizer instead.
    Yes the String.split is FAR better than the StringTokenizer class. The API even tells you to use the split method to do tokens.

Similar Threads

  1. charAt problem... Please help
    By Matija in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 02:20 AM
  2. I do not understand this error with charAt()
    By AcousticBruce in forum New To Java
    Replies: 20
    Last Post: 12-25-2010, 07:19 PM
  3. Space for charAt()?
    By Tussmann in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 07:57 PM
  4. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 06:27 PM
  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
  •