2017-02-22 185 views
0

我正在練習編寫Java代碼,我試圖解決這個問題,但我不知道如何解決它。我會告訴你我的代碼。線程「主」異常java.util.NoSuchElementException

在我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); 
    } 

在我FortuneCookieTest.java是

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

當我編譯並運行它:

//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) 

我怎樣才能解決這個問題?

回答

0

在這部分代碼中,您可以調用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

+0

謝謝你們的一切answer.I現在可以解決它。再次感謝你。 :) –

0

其實有與代碼中的幾個問題,特別是用這種方法getFortuneMsg()

  1. 您使用sWord所有的循環,而不是你應該使用sWord爲先循環oWord第二個和vWord和最後一個。
  2. 另外您在啓動循環之前調用line1 = subSt.nextToken("#");,以便您已經消耗了一個標記,這可能會產生NoSuchElementException我建議將此for (int i=0; i<sWord; i++)更改爲此for (int i=0; i<sWord - 1; i++)以考慮消耗的標記。
  3. 而對於這個循環while(subSt.hasMoreTokens())如果不是所有的標記都被消耗掉了(這可能發生在c1 < sWord),它會重新開始。

注意:代碼需要重構以防止重複並明智地使用循環。

編輯:我不明白到底是什麼,你要實現的目標,但如果我是你,我想這個方法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(); 
} 
+0

謝謝你所有的答案。我現在可以解決它,再次感謝你。 :) –

相關問題