• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

problem with ArrayList method

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic