Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.util.NoSuchElementException_Java - Fatal编程技术网

“线程中的异常”;“主要”;java.util.NoSuchElementException

“线程中的异常”;“主要”;java.util.NoSuchElementException,java,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

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

}
}
import java.util.Scanner;
导入java.io.*;
公共类可能内容{
公共静态void main(字符串[]args)抛出FileNotFoundException{
int numberofwords=0;
int-possiblesentence=0;
整数计数=0;
int POS=0;
int invalid=0;//变量
扫描仪扫描=新扫描仪(System.in);//新扫描仪
System.out.print(“请输入文件名:”);
String file1=scan.next();
文件文件=新文件(文件1);
扫描仪scan1=新扫描仪(文件);
while(scan1.hasNextLine()){
String line=scan1.nextLine();
扫描器句子=新扫描器(行);//扫描行
while(句子.hasNext()){
String word=scan1.next();//检查文件中的每个单词。
布尔值=假;
if(word.charAt(0)=word.charAt(word.length()-1))
{ 
identified=true;//florb
} 
if(word.indexOf(“cj”)!=-1 | | word.indexOf(“wq”)!=-1)
{ 
identified=true;//wooble
} 
如果((word.length()%2==1&&word.indexOf('z')==1)|
(word.length()%2==0&&word.indexOf('k')==1))
{ 
identified=true;//zith
} 

如果(word.charAt(word.length()-1)>=65&&word.charAt(word.length()-1)在最里面的循环中

String word = scan1.next();
我想你用错扫描仪了,应该是

String word = sentence.next();

A:描述您试图实现的目标。B:遇到异常时,发布堆栈跟踪的(相关部分)。