2

I encountered this error while using the haseNext() method for ArrayLists:

error: cannot find symbol
while(isduplicate == false && birthdays.hasNext())

this is my code:

import java.util.*;


class hello  
{  
public static void main(String args[])
{
    Integer size = 4;
    Integer count = 5;
    Integer doubleinarray = 0;

    for(Integer i = 0 ; i < count ; i++) {
        List<Integer> birthdays = new ArrayList<Integer>();
        birthdays = CreateSimulator(size);
        Integer countdown = size;
        boolean isduplicate = false;

        while(isduplicate == false && birthdays.hasNext()) {
            Integer date = birthdays.get(0);
            birthdays.remove(0);
            if(birthdays.contains(date)) {
                isduplicate = true;
                doubleinarray ++;
            }
        }
    }
    System.out.println(doubleinarray / count * 100);
}

public static List<Integer> CreateSimulator(int size)
{
    List<Integer> Birthdays = new ArrayList<Integer>(size);
    Random rand = new Random();

    for(Integer i =0 ; i < size ; i++) {
        Birthdays.add(rand.nextInt(364) + 1);
    }
    return Birthdays;
}
}    

I didn't understand why it doesn't accept the hasNext. besides this, the rest of the code works fine.

appreciate your help

thanks :)

Sagiv
  • 37
  • 6
  • 1
    Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – OH GOD SPIDERS Nov 01 '17 at 08:41
  • `birthdays` is a `List` and interface `List` does not have a `hasNext()` method. – Jesper Nov 01 '17 at 08:49

3 Answers3

3

you have to do something like:

Iterator<Integer> birthdaysIterator = birthdays.iterator();

And with the birthDaysIterator you can call hasNext.

But this is not recommended nowadays. You are better of performing a normal for, like:

with normal for:

for (int i = 0; i < birthdays.size(); i++) {
    System.out.println(birthdays.get(i));
}

with for-each loop:

for (Integer birthday : birthdays) {
   System.out.println(birthday);
}

with Java 8 streams:

birthdays.forEach((birthday) -> {
            System.out.println(birthday);
});

EDIT:

Per @OHGODSPIDERS, if you use the other 3 versions that I suggested, you will run into ConcurrentModificationException. In order to avoid that, you can either stick with your iterator, or you can use an intermediate list to keep the elements which you want to delete, and remove them all afterwards.

Example:

List<String> toRemove = new ArrayList<>();
for (String birthday : birthdays) {
    if (someCondition) {
        toRemove.add(birthday);
    }
}
birthdays.removeAll(toRemove);
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
  • better yet: use a `foreach`-loop =) – Turing85 Nov 01 '17 at 08:46
  • well, I didn't want to go that far. I mean you can straight ahead use `stream` for this :). But yeah, I will update my answer with the other 2 versions. – Andrei Sfat Nov 01 '17 at 08:47
  • The asker is trying to remove elements from the list until it is empty. Neither iterating with a for nor a foreach loop will work in this case (Will get ConcurrentModificationException) and beside that he should probably just use isEmpty() as a condition in his loop. – OH GOD SPIDERS Nov 01 '17 at 08:48
  • @OHGODSPIDERS, added an edit per your suggestion. Let me know if you think it's fine or needs improvement. Thanks – Andrei Sfat Nov 01 '17 at 08:56
1

birthdays is of type List which does not have a method of that name. What you are looking for is the iterator, which you can access like this: Iterator<Integer> iterator = birthdays.iterator() And use it to traverse the list. hasNext is a method of the type Iterator.

Stav Saad
  • 589
  • 1
  • 4
  • 13
1

As mentioned, the List class does not have a hasNext() method

An alternate way to use this, would be to check if it is not empty

while (isduplicate == false && !birthdays.isEmpty()) {
achAmháin
  • 4,176
  • 4
  • 17
  • 40