Chris Olszewski
Chris Olszewski

Reputation: 151

I keep getting exception in thread "main" java.util.NoSuchElementException for arrayList

Whenever I run my code which is supposed to analyze a text input I get the following output. The other methods used to count words and alphanumeric digits works but the other method which is supposed spit out words with five or more characters does not work. I keep getting an error that says no such element which I guess means that the iterator can't find any more elements but that shouldn't be happening because I use a while statement.

Here is the output:

 Enter text 
 running
 Word count: 1
 Alphanumeric count: 7
 Words in ascending order 
 running
 Words with five or more characters 
 Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(Unknown Source)
at com.yahoo.chris511026.paragraphcount.ManifoldMethod.wordSort(ManifoldMethod.java:55)
at com.yahoo.chris511026.paragraphcount.ParagraphAnalyzer.main(ParagraphAnalyzer.java:15)

My code:

package com.yahoo.chris511026.paragraphcount;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class ManifoldMethod { 

static ArrayList<String> stringList = new ArrayList<String>();

public static void wordCount(String data) {

        int counter = 0;

            for (String str : data.split("[^a-zA-Z-']+")) {

                stringList.add(str);
                counter++;

            }

        System.out.println("Word count: " + counter);

}


public static void alphanumericCount(String data) {

    data = data.replaceAll("[[^a-z]&&[^A-Z]&&[^0-9]]", "");

    System.out.println("Alphanumeric count: " + data.length());

}

public static void wordSort(String data) { 

    Collections.sort(stringList, new StringComparator());

    System.out.println("Words in ascending order ");  

        for (String s: stringList)

            System.out.println(s);

    System.out.println("Words with five or more characters ");

        int count=0;

        Iterator<String> itr=stringList.iterator();

            while(itr.hasNext())

 if (itr.next().replaceAll("[[^a-z]&&[^A-Z]&&[^0-9]]", "").length  ()>=5)         {

                    System.out.println(itr.next());
                    count++;

                }

                if (count==0) {

                    System.out.println("None.");

                }

}

  }

EDIT:

I corrected it and use String str=itr.next(); here is the new secton. But now I get that the String variable cannot be resolved to a variable. Why?

while(itr.hasNext())
    String str=itr.next();  
    if (str.replaceAll("[[^a-z]&&[^A-Z]&&[^0-9]]", "").length  ()>=5) {
        System.out.println(str);
        count++;
    }

Upvotes: 0

Views: 4867

Answers (2)

Aleksander Blomsk&#248;ld
Aleksander Blomsk&#248;ld

Reputation: 18552

You call itr.next() twice inside your while method. The second time it is being called, the exception is thrown (as there are no more elements there. You should probably rewrite your while-loop to something like this:

while(itr.hasNext()) {
    String s = itr.next();
    if (s.replaceAll("[[^a-z]&&[^A-Z]&&[^0-9]]", "").length  ()>=5)         {
        System.out.println(s);
        count++;
    }
    if (count==0) {
        System.out.println("None.");
    }
}

Upvotes: 0

NPE
NPE

Reputation: 500457

The problem is that you're calling next() twice:

       while(itr.hasNext())

           if (itr.next().replaceAll("[[^a-z]&&[^A-Z]&&[^0-9]]", "").length  ()>=5)         {
           //      ^^^^^^

                System.out.println(itr.next());
                //                     ^^^^^^

This means that when the if condition is satisfied, the body of the if attempts to operate on the element that comes after the one that has matched.

You need to call it once per iteration and store the result in a variable.

Upvotes: 2

Related Questions