主线程中的异常没有这样的元素异常

时间:2016-02-22 15:29:41

标签: java

我对Java很陌生,我在弄清楚程序出错的地方时遇到了一些麻烦。我有它所以它正在做一个疯狂的libs类似的东西,它读取带有问题(或类别)的文档,然后提示循环的答案,直到它有所有的答案。它将这些答案提交给名为“answers”的文本文件,然后读取文件并打印一条消息,以及包含完整madlib的另一个文件。

编译时我实际上并没有收到错误,但是在输入了所有答案之后

线程“main”中的异常java.util.NoSuchElementException:找不到行

-at java.util.Scanner.nextLine(Scanner.java:1540) 
-at reader.main(reader.java:68) 

以下是完整的参考代码

import java.util.Scanner;                                        //     importing scanner object for usage
import java.io.*;
import java.io.PrintWriter;
public class reader{
    public static void main(String[] args) throws IOException                   
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("enter the name of a file");
    String filename = keyboard.nextLine();

    File file = new File(filename);
    Scanner inputFile = new Scanner(file);




    int limit = inputFile.nextInt();
    int n;
    inputFile.nextLine();
    PrintWriter answers = new PrintWriter("answers.txt");

    for(n = 0; n < limit; n++)
        {
        String line = inputFile.nextLine();
        System.out.println(line);
        String answer = keyboard.nextLine();
        answers.println(answer);
        }



    inputFile.close();
    answers.close();

    File useanswers = new File("answers.txt");
    Scanner inputFile2 = new Scanner(useanswers);
    String outputline = inputFile2.nextLine();
    String outputline2 = inputFile2.nextLine();
    String outputline3 = inputFile2.nextLine();
    String outputline4 = inputFile2.nextLine();
    String outputline5 = inputFile2.nextLine();
    String outputline6 = inputFile2.nextLine();
    String outputline7 = inputFile2.nextLine();
    String outputline8 = inputFile2.nextLine();
    String outputline9 = inputFile2.nextLine();
    String outputline10 = inputFile2.nextLine();
    String outputline11 = inputFile2.nextLine();
    String outputline12 = inputFile2.nextLine();
    String outputline13 = inputFile2.nextLine();
    String outputline14 = inputFile2.nextLine();
    String outputline15 = inputFile2.nextLine();
    String outputline16 = inputFile2.nextLine();
    String outputline17 = inputFile2.nextLine();
    String outputline18 = inputFile2.nextLine();
    String outputline19 = inputFile2.nextLine();

    PrintWriter result = new PrintWriter("Madlibs_result.txt"); 
    System.out.println("Batman is " + outputline + ". Teenager " + outputline2 + 
     "was traumatized by " + outputline3 + "his parent's murder and vowed to " + outputline4 +
    " their deaths by bringing the " + outputline5 + " to justice. " + outputline6 + " used his " +
    outputline7 + "fortune to study criminology, to train his body to "  + outputline8 + " perfection, " +
    "and to acquire hight tech vehicles and "  + outputline9 + " to fight crime in his homw town of " + outputline10 + ". One night " + 
     outputline11 + "was " + outputline + " by a bat outside his window and decided to dress himself as a \"bat man\" to strike " +
      outputline12 + " in the \"" + outputline13 + " and "  + outputline14 + "\" hearts of "  + outputline15 + ". From that moment forward, " +
      outputline16 + " became \"Batman\" in his altered "  + outputline17);

    result.println("Batman is " + outputline + ". Teenager " + outputline2 + 
     "was traumatized by " + outputline3 + "his parent's murder and vowed to " + outputline4 +
    " their deaths by bringing the " + outputline5 + " to justice. " + outputline6 + " used his " +
    outputline7 + "fortune to study criminology, to train his body to "  + outputline8 + " perfection, " +
    "and to acquire hight tech vehicles and "  + outputline9 + " to fight crime in his homw town of " + outputline10 + ". One night " + 
     outputline11 + "was " + outputline + " by a bat outside his window and decided to dress himself as a \"bat man\" to strike " +
      outputline12 + " in the \"" + outputline13 + " and "  + outputline14 + "\" hearts of "  + outputline15 + ". From that moment forward, " +
      outputline16 + " became \"Batman\" in his altered "  + outputline17);


    result.close();
    }
}

1 个答案:

答案 0 :(得分:0)

已解决,我有太多的String outputline ...行,我假设当它试图转到创建的文本文件中不存在的第19行和第20行时,它给了我一个错误。