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

时间:2013-10-30 18:47:20

标签: java

尝试获取下一个单词时出错,我在其中检查文件的每个单词。任何想法?
错误是线程“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);

}
}

1 个答案:

答案 0 :(得分:1)

在最里面的循环里面......

String word = scan1.next();

我认为您使用的是错误的扫描仪。它应该是

String word = sentence.next();