-1

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

}
}
4

1 回答 1

1

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

String word = scan1.next();

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

String word = sentence.next();
于 2013-10-30T18:50:48.883 回答