Results 1 to 7 of 7
Thread: Why won't charAt() work?
- 08-04-2011, 03:58 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
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++; } }
- 08-04-2011, 04:09 AM #2
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
Never Mind, just used a StringTokenizer instead.
- 08-04-2011, 04:22 AM #3Eclipse keeps telling me it is not a valid method for my class
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);
- 08-04-2011, 04:24 AM #4
In future post the full and exact error message not your paraphrased interpretation.
- 08-04-2011, 05:46 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Also, you should avoid using string tokenizer and stick to split instead. StringTokenizer is a legacy class only still around for old code.
- 08-04-2011, 10:43 AM #6
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.
- 08-04-2011, 10:46 AM #7
Similar Threads
-
charAt problem... Please help
By Matija in forum New To JavaReplies: 7Last Post: 01-21-2011, 02:20 AM -
I do not understand this error with charAt()
By AcousticBruce in forum New To JavaReplies: 20Last Post: 12-25-2010, 07:19 PM -
Space for charAt()?
By Tussmann in forum New To JavaReplies: 5Last Post: 11-02-2010, 07:57 PM -
Help with charAt()
By HackerOfDoom in forum New To JavaReplies: 7Last Post: 03-21-2010, 06:27 PM -
Help With Input.charAt(LastIndex);
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:22 AM
Bookmarks