Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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_Algorithm_Loops_If Statement_Java.util.scanner - Fatal编程技术网

错误:java.util.NoSuchElementException-扫描程序未按预期运行

错误:java.util.NoSuchElementException-扫描程序未按预期运行,java,algorithm,loops,if-statement,java.util.scanner,Java,Algorithm,Loops,If Statement,Java.util.scanner,扫描仪返回NoSuch元素异常错误。你能解释一下为什么会这样吗 扫描仪现在通过并正常运行,但它没有从第二次扫描仪调用中获取下一行输入。这可能是一个小小的调整,但有人能指出错误是什么吗 public class JavaHW1_1 { private static Scanner userInput = new Scanner(System.in); public static void main(String[] args) throws IOException { String

扫描仪返回NoSuch元素异常错误。你能解释一下为什么会这样吗

扫描仪现在通过并正常运行,但它没有从第二次扫描仪调用中获取下一行输入。这可能是一个小小的调整,但有人能指出错误是什么吗

public class JavaHW1_1 {

private static Scanner userInput = new Scanner(System.in);


public static void main(String[] args) throws IOException {

    String pattern ;
    String fileName = null;



    //      Method to manage user inputs 
    fileName = userInputFileName(userInput);
    pattern = userInputPattern(userInput);

    //      To find the pattern in the file
    //      findPattern();

}


private static String userInputPattern(Scanner userInput) {
    String pattern = "JollyGood";
    System.out.println(". Please enter a pattern to find in the file");

    while(userInput.hasNextLine()){
        pattern = userInput.nextLine();
        System.out.println("The pattern to be searched: "+ pattern);
    }
    userInput.close();

    return pattern;
}


private static String userInputFileName(Scanner userInput) throws IOException {
    String path = "./src";
    String files, fileName;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    System.out.println("Please input the desired file name:\n");
    System.out.println("Some suggestions:\n");
     for (int i = 0; i < listOfFiles.length; i++) 
      {

       if (listOfFiles[i].isFile() && listOfFiles[i].getName().toLowerCase().endsWith(".txt")) 
       {

       files = listOfFiles[i].getName();
       System.out.println(files);
          }
      }

     int userAttempt = 0;

     do{
     fileName = userInput.nextLine();

     if(fileName.toLowerCase().endsWith(".txt")){
         System.out.println("The file name entered is in correct format");
         File file = new File("./src",fileName);

         try {
            file.createNewFile();
            System.out.println("File is created. Please enter text to be written in the file. End the content with \"eof\"");
            InputOutput(file.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }

         userAttempt = 10;
     }
     else
         {System.out.println("Please enter correct format file with .txt extension");
         userAttempt++;}
     }while (userAttempt <10);

    return fileName;
}




private static void InputOutput(String fName) throws IOException {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter("./src/" + fName));
        String inputLine = null;
        do {
            inputLine=in.readLine();
            out.write(inputLine);
            out.newLine();
        } while (!inputLine.equalsIgnoreCase("aaa"));
        System.out.print("Write Successful");
    } catch(IOException e1) {
        System.out.println("Error during reading/writing");
    } finally {
        out.close();
        in.close();
    }

}


private static void findPattern() {
    // TODO Auto-generated method stub

}


}
公共类JavaHW1_1{
专用静态扫描仪userInput=新扫描仪(System.in);
公共静态void main(字符串[]args)引发IOException{
字符串模式;
字符串文件名=null;
//管理用户输入的方法
fileName=userInputFileName(userInput);
模式=userInputPattern(userInput);
//在文件中查找模式
//findPattern();
}
私有静态字符串userInputPattern(扫描程序userInput){
字符串模式=“JollyGood”;
System.out.println(“.请输入要在文件中查找的模式”);
while(userInput.hasNextLine()){
pattern=userInput.nextLine();
System.out.println(“要搜索的模式:“+模式”);
}
userInput.close();
回报模式;
}
私有静态字符串userInputFileName(扫描程序userInput)引发IOException{
字符串路径=“./src”;
字符串文件,文件名;
文件夹=新文件(路径);
File[]listOfFiles=folder.listFiles();
System.out.println(“请输入所需的文件名:\n”);
System.out.println(“一些建议:\n”);
for(int i=0;i}while(usertrument如果将EOF直接传递到标准输入中,则可能发生此情况。例如(在windows中):

上面的^Z表示windows命令提示符上的Ctrl-Z,这是一个EOF信号

您需要考虑您的需求和过程/显示错误,如果用户提供了EOF而没有任何先前的数据

,那么,您可能会关闭<代码>扫描器< /代码>,并创建一个新的来从<代码>系统中读取。在中,查看代码是有意义的。 因此,我建议您的代码通过参数接收扫描仪,如下所示:

public static void main(String[] args){

    Scanner scan = new Scanner (System.in);
    String pattern = userInputPattern(scan);
    String test = readSomethingElse(scan);
}

private static String readSomethingElse(Scanner scan) {
   System.out.println(". Read something else");
    return scan.nextLine();
}

private static String userInputPattern(Scanner scan) {

    String pattern = "JollyGood";
    System.out.println(". Please enter a pattern to find in the file");
    pattern = scan.nextLine();
    System.out.println("The pattern to be searched: "+ pattern);
    return pattern;
}

请张贴stacktrace。请检查此项,以便…您是否用其他方法打开扫描仪?很抱歉,可能是…的副本,但我无法拒绝发表评论。
SuccessfulException
是一个矛盾修饰法,如果我读过的话。如果您可以控制该类,请重命名该类。看起来您可能是正确的,但我应该如何更正它。因为如果我用任何其他字符串“eof”或“;”或“aaa”结束上一个扫描仪中的内容它仍然抛出相同的错误。啊,很好的一个错误。我同意,可能扫描器有一个缓冲区支持,如果您尝试使用多个扫描器进行扫描,输入可能已经被前一个扫描器的缓冲区“吃掉”。如果问题是开放的
Scanner
s,那么最好执行
try{…}最后{[close Scanner]}
或使用Java 7的自动资源管理?@jpmc26我没有玩过Java 7,所以不知道这将如何工作…关于try/最后,这是一个有趣的解决方案,我将编辑它并添加到答案中作为第二个选项。这很有帮助,因此我没有收到任何错误,但它没有再次请求输入。我的扫描输入不是简单地说,在一个方法中定义,一个方法中有多个输入。@jpmc26关于try/finally解决方案,它不起作用,因为当扫描仪关闭时,System.in关闭。
public static void main(String[] args){

    Scanner scan = new Scanner (System.in);
    String pattern = userInputPattern(scan);
    String test = readSomethingElse(scan);
}

private static String readSomethingElse(Scanner scan) {
   System.out.println(". Read something else");
    return scan.nextLine();
}

private static String userInputPattern(Scanner scan) {

    String pattern = "JollyGood";
    System.out.println(". Please enter a pattern to find in the file");
    pattern = scan.nextLine();
    System.out.println("The pattern to be searched: "+ pattern);
    return pattern;
}