File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problem with ArrayList method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with ArrayList method" Watch "problem with ArrayList method" New topic

problem with ArrayList method

Erkki Veps�l�inen

Joined: Feb 10, 2004
Posts: 9
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;
while (j > st1.length() || laskuri > linelength);
why's that?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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 ]

[Jess in Action][AskingGoodQuestions]
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
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?

My theory of evolution is that Darwin was adopted. - Steven Wright
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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:

I agree. Here's the link:
subject: problem with ArrayList method
It's not a secret anymore!