简体   繁体   English

线程“主”中的异常java.util.NoSuchElementException

[英]Exception in thread “main” java.util.NoSuchElementException

I'm practice to write java code and i try to fix this but i don't know how to fix it anymore. 我正在练习编写Java代码,并且尝试解决此问题,但是我不知道如何解决它。 I'll show you my code. 我将向您展示我的代码。

In my Fortunecookie.java is: 在我的Fortunecookie.java中是:

public class FortuneCookie {
    private String subjectList;
    private String objectList;
    private String verbList;
    private int sWord;
    private int oWord;
    private int vWord;
    private Random random = new Random() enter code here;

    public FortuneCookie() {
        subjectList = "I#You#He#She#It#They";
        objectList = "me#you#him#her#it#";
        verbList = "hate#love#deny#find#hear#forgive#hunt#win#teach";
    }

    public String getFortuneMsg() {
        StringTokenizer subSt = new StringTokenizer(subjectList,"#");
        StringTokenizer objSt = new StringTokenizer(objectList,"#");
        StringTokenizer verbSt = new StringTokenizer(verbList,"#");
        sWord = subSt.countTokens();
        oWord = objSt.countTokens();
        vWord = verbSt.countTokens();
        int c1 = random.nextInt(sWord);
        String line1 = " ";
        String line2 = " ";
        String line3 = " ";
        while(subSt.hasMoreTokens()) {
            line1 = subSt.nextToken("#");
            for (int i=0;i<sWord;i++)
                if (i == c1) {
                    break;
                }
                else{
                    line1 = subSt.nextToken("#");
                }
        }
        int c2 = random.nextInt(oWord);
        while(objSt.hasMoreTokens()) {
            line2 = objSt.nextToken("#");
            for (int i=0;i<sWord;i++)
                if (i == c2) {
                    break;
                }
                else{
                    line2 = objSt.nextToken("#");
                }
        }
        int c3 = random.nextInt(vWord);
        while(verbSt.hasMoreTokens()) {
            line3 = verbSt.nextToken("#");
            for (int i=0;i<sWord;i++)
                if (i == c3) {
                    break;
                }
                else{
                    line3 = verbSt.nextToken("#");
                }
        }
        return line1+line2+line3;
    }

    public void setSubjectList(String aSubjectList) {
        subjectList = aSubjectList;
    }

    public void setObjectList(String aObjectList) {
        objectList = aObjectList;
    }

    public void setVerbList(String aVerbList) {
        verbList = aVerbList;
    }

    public void print() {
        StringTokenizer subSt = new StringTokenizer(subjectList,"#");
        StringTokenizer objSt = new StringTokenizer(objectList,"#");
        StringTokenizer verbSt = new StringTokenizer(verbList,"#");
        sWord = subSt.countTokens();
        oWord = objSt.countTokens();
        vWord = verbSt.countTokens();
        System.out.println("Subject List : "+subjectList);
        System.out.println("Object List : "+objectList);
        System.out.println("Verb List : "+verbList);
    }

And in my FortuneCookieTest.java is 在我的FortuneCookieTest.java中

public class FortuneCookieTest {
    public static void main(String[] args) {
        FortuneCookie ck = new FortuneCookie();
        System.out.println(ck.getFortuneMsg());
    }
}

And when I compile and run it: 当我编译并运行它时:

//Exception in thread "main" java.util.NoSuchElementException
        at java.util.StringTokenizer.nextToken(Unknown Source)
        at java.util.StringTokenizer.nextToken(Unknown Source)
        at CS_111_Homework_2.FortuneCookie.getFortuneMsg(FortuneCookie.java:39)
        at CS_111_Homework_2.FortuneCookieTest.main(FortuneCookieTest.java:6)

How can I solve it? 我该如何解决?

In this part of the code, you can call objSt.nextToken("#") twice, and if the first calls gets the last element, on the second call you'll get the NoSuchElementException as no more elements are available. 在此部分代码中,您可以调用objSt.nextToken("#")两次,如果第一次调用获得了最后一个元素,则在第二次调用时将获得NoSuchElementException因为没有更多的元素可用。

while (objSt.hasMoreTokens()) {
    line2 = objSt.nextToken("#");
    for (int i = 0; i < sWord; i++)
        if (i == c2) {
            break;
        } else {
            line2 = objSt.nextToken("#");
        }
}

This is a different use case, but has the same problem 这是一个不同的用例,但存在相同的问题

Actually there are several issues with the code, specially with this method getFortuneMsg() : 实际上,代码存在一些问题,特别是使用此方法getFortuneMsg()

  1. you are using sWord for all the loops, instead you should use sWord for the first loop oWord for the second and vWord and for the last. 您将sWord用于所有循环,而应将sWord用于第一个循环,将oWord用于第二个循环,将vWord和最后一个循环使用。
  2. Also you are calling line1 = subSt.nextToken("#"); 另外,您正在调用line1 = subSt.nextToken("#"); before starting the loop so you have consumed one token already, this could produce NoSuchElementException i suggest to change this for (int i=0; i<sWord; i++) to this for (int i=0; i<sWord - 1; i++) to take in consideration the consumed token. 在开始循环之前,您已经消耗了一个令牌,这可能会产生NoSuchElementException我建议将for (int i=0; i<sWord; i++)更改for (int i=0; i<sWord - 1; i++)以考虑消耗的令牌。
  3. And for this loop while(subSt.hasMoreTokens()) it will be started all over again if not all tokens are consumed (it could be happen when c1 < sWord ) . 对于此循环while(subSt.hasMoreTokens()) ,如果不是所有令牌都被消耗,它将重新开始(这可能在c1 < sWord时发生)。

Note: The code need some refactoring to prevent duplication and use loops wisely. 注意:代码需要一些重构,以防止重复并明智地使用循环。

Edit: I didn't understand exactly what you are trying to achieve but if I were you I would like to change this method getFortuneMsg() to something like this: 编辑:我不完全了解您要实现的目标,但是如果您是我,我想将此方法getFortuneMsg()更改为以下内容:

 public String getFortuneMsg() {

    StringTokenizer[] tokenizers = {new StringTokenizer(subjectList, "#"), new StringTokenizer(objectList, "#"), new StringTokenizer(verbList, "#")};

    StringBuilder sb = new StringBuilder();
    for (StringTokenizer tokenizer : tokenizers) {

        int rCount = random.nextInt(tokenizer.countTokens());

        for (int i = 0; i < rCount; i++) {
            tokenizer.nextToken();
        }
        sb.append(tokenizer.nextToken());
    }
    return sb.toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主”中的异常java.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 主线程java.util.NoSuchElementException中的异常 - Exception in main thread java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException - Exception in thread "main" java.util.NoSuchElementException Java扫描器错误:线程“主”中的异常java.util.NoSuchElementException - Java Scanner Error: Exception in thread “main” java.util.NoSuchElementException Java中线程“main”java.util.NoSuchElementException中的异常 - Exception in thread "main" java.util.NoSuchElementException in Java java 新手 - 线程“main”中的异常 java.util.NoSuchElementException - New to java - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的Java异常java.util.NoSuchElementException运行时错误 - Java Exception in thread “main” java.util.NoSuchElementException runtime error Java错误-“线程“ main”中的异常” java.util.NoSuchElementException - Java Error - “Exception in thread ”main" java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException,Java 扫描器 - Exception in thread "main" java.util.NoSuchElementException, Java scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM