0

Scanner returning NoSuch Element Exception error. Could you explain why is this happening.

The Scanner now passes and runs fine but it didn't take the nextLine input from the second Scanner call. This may be a little tweak but could someone point out what the mistake is.

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

}


}
4

2 回答 2

1

基于SO,您可能正在关闭Scanner并创建一个新的以从中读取,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;
}
于 2013-06-11T05:33:25.077 回答
0

如果您将 EOF 直接传递到标准输入中,则可能会发生这种情况。例如(在 Windows 中):

java com.myprog.MainClass
^Z
Exception in thread "main" java.util.NoSuchElementException: No line found
  at java.util.Scanner.nextLine(Unknown Source)
  ....

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

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

于 2013-06-11T05:25:15.053 回答