2013-10-30 127 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(); 
相关问题