1

Why am I receiving the following error: cannot find symbol: method add? This is my code:

import java.util.*

public class ArrayList {
    // instance variables - replace the example below with your own
    public void processinput(String s) {
        int[] a = { 34, 25, 16, 98, 77, 101, 24 };

        ArrayList b = new ArrayList();

        for (int i = 0; i < a.length; i++) {
            int d = a[i];
            if (d % 2 > 0) {
                b.add(new Integer(d));
            }
        }

        for (int i = 0; i < b.size(); i++) {
            System.out.print(b.get(i) + " ");
        }

        for (int i = a.length - 1; i >= 0; i--) {
            System.out.print(a[i] + " ");
        }
    }
}

How can I resolve this error?

2
  • 2
    Your code compiles for me (if I put it inside a method in a class, obviously). Do you possibly have a class named ArrayList somewhere? If so you might accidentally using that instead of java.util.ArrayList, which you probably meant. Commented Jan 25, 2011 at 10:29
  • 1
    possible duplicate of cannot find symbol-method add (java.lang.integer)..whats the problem actually? Commented Jan 26, 2011 at 7:55

3 Answers 3

3

Reading your question again, I assume you are missing

import java.util.*;

at the start of your class. This is the only way you could be getting this error. BTW you should also be getting an error like "Cannot resolve symbol ArrayList" which tells you what the problem is.


Change the last loop to be

for(int i = a.length-1; i >= 0; i--) {

With experience you will be able to read error messages and understand what they mean. In this case

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Main.main(Main.java:33)

On line 33, (or what ever it is in your code) you tried to accessed an index which is beyond the end of the array. The last element of array a is a.length-1 because the first element is 0

It is important to read error message carefully because they are often the best clue as to what the problem is and how to fix it.

When you post a question you should post the error you are getting.

Sign up to request clarification or add additional context in comments.

4 Comments

It's not very visible from the post, but he's actually fighting a compiler error at the moment.
@Joachim, I got that from your comment. I think its a missing import and he's not reading all the error messages. ;)
I vote for the missing (or wrong) import. (and for your answer ;) )
If you see a follow up question, the OP has named the class ArrayList. ;) Didn't see that one comming.
1

There is ArrayIndexOutOfBoundsException: 7 . fix this line

for(int i = a.length-1; i >= 0; i--)

Comments

0

Your class is called ArrayList, and this is hiding java.util.ArrayList, hence your compilation errors. Either change the name of your class, or use the full name of java.util.ArrayList when declaring b.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.