0

スキャナが NoSuch Element Exception エラーを返しました。なぜこれが起こっているのか説明していただけますか。

Scanner はパスして正常に実行されるようになりましたが、2 回目の Scanner 呼び出しから nextLine 入力を受け取りませんでした。これは少し微調整かもしれませんが、誰かが間違いを指摘できます。

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

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 は、EOF シグナルである Windows コマンド プロンプトの Ctrl-Z を表します。

ユーザーに事前データなしでEOFが提供された場合は、要件とプロセス/表示エラーを考慮する必要があります

于 2013-06-11T05:25:15.053 に答える