2013-10-30 51 views
-2

嘗試獲取下一個單詞時出現錯誤,其中檢查文件的每個單詞。有任何想法嗎?
錯誤是在線程異常 「主要」 java.util.NoSuchElementException線程「main」中的異常java.util.NoSuchElementException

import java.util.Scanner; 
import java.io.*; 
public class PossibleSentence{ 
    public static void main(String[] args) throws FileNotFoundException{ 

    int numberofwords = 0; 
    int possiblesentence = 0; 
    int count = 0; 
    int POS = 0; 
    int invalid = 0; //Varriables 

    Scanner scan = new Scanner(System.in); //New scanner 

    System.out.print("Please enter the name of a file: "); 

    String file1 = scan.next(); 


    File file = new File(file1); 

    Scanner scan1 = new Scanner(file); 



    while(scan1.hasNextLine()){ 

     String line = scan1.nextLine(); 

     Scanner sentence = new Scanner(line); //scans the line 


     while(sentence.hasNext()) { 

      String word = scan1.next(); //checking each word of the file. 

      boolean identified = false; 

      if(word.charAt(0) == word.charAt(word.length() - 1)) 
      { 
       identified = true; //florb 

      } 

      if(word.indexOf("cj") != -1 || word.indexOf("wq") != -1) 
      { 
       identified = true; //wooble 

      } 

      if((word.length() % 2 == 1 && word.indexOf('z') == -1) || 
       (word.length() % 2 == 0 && word.indexOf('k') == -1)) 
     { 
       identified = true; //zith 

      } 

      if(word.charAt(word.length() - 1) >= 65 && word.charAt(word.length() - 1) <= 90) 
      { 
       identified = true; //zarf 

      } 

      if(identified) 
      { 
       POS++;//part of speech 
      } 

     numberofwords++; //checking how many there are 
    } 

    if(POS==numberofwords){ 
     possiblesentence++; //possible sentence 
    } 

    else{ 
     invalid++; //not a sentence 
    } 

    } 

    System.out.println("Number of possibly valid sentences: " + possiblesentence); 
     System.out.println("Number of invalid sentences: " + invalid); 

} 
} 
+0

答:描述你想要達到的目標。 B:遇到異常時,張貼堆棧跟蹤(的相關部分)。 – haraldK

回答

1

裏面的最內層循環...

String word = scan1.next(); 

我覺得你使用了錯誤的掃描儀。它應該是

String word = sentence.next(); 
相關問題