Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with ArrayList method

 
Erkki Veps�l�inen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
I'm trying to move characters from a string (st) to an element of an array, using alist1.add() method. I get an error message "cannot resolve symbol" with this:
)
do {
alist1.add(i, st.charAt(j));
laskuri=st.length() + 1;
j++;
}
while (j > st1.length() || laskuri > linelength);
why's that?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the answer depends on whether you're dealing with an ArrayList or an array, two very different things.
If you're dealing with an array, they don't have any "add" methods -- they very have few methods at all, just the ones inherited from Object (equals, hashCode, etc.) You use "array1[j] = st.charAt(1)" to set an element.
If you're dealing with an ArrayList, you're trying to pass a char to a method add(int, Object), but a char is not an Object. You'd have to convert the char to a String or other object -- precisely what you should do depends on what you're after, exactly.
Now, two other things: first, you ought never to use a "do... while" loop when a "for" loop would work just as well; the "for" loop will be much clearer. Unfortunately, I can't quite figure out the details of this loop's exit condition, so I won't try to translate it for you.
Finally, note that String has a toCharArray() method which returns the String as an array of characters; this method may be useful to you.
[ February 11, 2004: Message edited by: Ernest Friedman-Hill ]
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you declared and instantiated all of your variables, i.e. is there a line in your code somewhere like

Is it i, j, st, laskuri or linelength that the compiler doesn't like? Which symbol can not be resolved?
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best advice I can give: Know thy standard libraries.
To transfer characters from a String to a char[]:

To transfer characters from a String to an ArrayList:

sev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic