【问题标题】:I keep getting exception in thread "main" java.util.NoSuchElementException for arrayList我在arrayList的线程“main”java.util.NoSuchElementException中不断收到异常
【发布时间】:2013-01-21 19:47:45
【问题描述】:

每当我运行应该分析文本输入的代码时,我都会得到以下输出。用于计算单词和字母数字数字的其他方法有效,但假设吐出具有五个或更多字符的单词的其他方法不起作用。我不断收到一个错误,说没有这样的元素,我猜这意味着迭代器找不到更多元素,但这不应该发生,因为我使用了 while 语句。

这是输出:

输入文字 跑步 字数:1 字母数字计数:7 按升序排列的单词 跑步 有五个或更多字符的单词 线程“主”java.util.NoSuchElementException 中的异常 在 java.util.AbstractList$Itr.next(未知来源) 在 com.yahoo.chris511026.paragraphcount.ManifoldMethod.wordSort(ManifoldMethod.java:55) 在 com.yahoo.chris511026.paragraphcount.ParagraphAnalyzer.main(ParagraphAnalyzer.java:15)

我的代码:

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.");

                }

}

  }

编辑:

我更正了它并使用String str=itr.next(); 这是新的部分。但现在我知道String 变量无法解析为变量。为什么?

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

【问题讨论】:

  • 请发布完整的堆栈跟踪。
  • 好的,我会发布完整的堆栈跟踪
  • @madth3 - 我现在拿到了。

标签: java arraylist


【解决方案1】:

问题是你调用了两次next()

       while(itr.hasNext())

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

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

这意味着当满足if 条件时,if 的主体会尝试对匹配的元素之后进行操作。

您需要每次迭代调用一次并将结果存储在变量中。

【讨论】:

    【解决方案2】:

    您在 while 方法中调用 itr.next() 两次。第二次调用它时,抛出异常(因为那里没有更多元素。您可能应该将您的 while 循环重写为如下内容:

    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.");
        }
    }
    

    【讨论】:

    • 我不能说当我执行此操作时无法解析为局部变量 String str = itr.next();
    • 它不让我说同样的话
    • while 之后缺少大括号{
    • 我在工作中强加给我的最好的规则之一是一个老板要求我们在 if 语句和 while/for 循环之后总是使用大括号(即使块只包含一个语句)。省略牙套会让婴儿耶稣哭泣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多