BufferedReader.readLine()在文件的开头而不是结尾返回null

时间:2011-10-23 18:14:18

标签: java readline bufferedreader

我正在为Java课程创建一个真/假测试,该课程将答案键和每个用户各自的答案数组存储为BitSet。然后将这些BitSet序列化为持久性顺序二进制文件,以便稍后可以由另一个应用程序对它们进行评分。

我的第一次调用request.readLine()的奇怪例外是一切都很完美,system.in是我的public class CreateTest { private static BufferedReader request; private static BufferedReader response; private static String answers, userName; private static ObjectOutputStream result; public static void main(String[] args) { response = new BufferedReader(new InputStreamReader(System.in)); try { request = new BufferedReader(new FileReader("test.txt")); } catch(FileNotFoundException fnfe) { System.out.println("Test.txt was not found. Please fix your crappy file system."); System.exit(1); } System.out.println("Welcome to THE TEST\n\n" + "Please respond with only \"t\" for true or \"f\" for false.\n" + "This application is case-insensitive.\n" + "DON'T GET EATEN BY THE GRUE!"); System.out.println("\nPlease enter your name: "); try { userName = response.readLine().replaceAll("\\s", ""); System.out.println("\n\n"); result = new ObjectOutputStream(new FileOutputStream(userName + "TestAnswers.bin")); } catch(IOException e1) { e1.printStackTrace(); } try { for(int i=0; i<24; i++) { System.out.println(request.readLine()); recordResponse(); } System.out.println("Thank you for attempting THE TEST. You probably failed."); result.writeObject(new BitMap(answers)); close(); } catch (IOException e) { e.printStackTrace(); } } public static void recordResponse() throws IOException { String currentAnswer = response.readLine(); //diagnostic System.out.println("Answer: " + answers); if(currentAnswer.equals("t")|| currentAnswer.equals("T")|| currentAnswer.equals("f")|| currentAnswer.equals("F")) { answers += currentAnswer + " -- "; } else { System.out.println("What, you can't read or somethin'?. Enter(case-insenstive) T or F only pal." + "Try it again."); close(); System.exit(1); } } public static void close() throws IOException { request.close(); response.close(); } 输入流读取器,可以检索用户的答案。出于某种原因,无论输入什么,它都会将第一个答案设置为null,就像它已经达到EOF一样。

CreateTest.java (从txt文件中读取/显示问题,收集用户答案,将其存储在.bin文件中)

public BitMap(String s) throws IndexOutOfBoundsException,ArithmeticException
{
    try
    {
        if(s.length() > 25) { throw new IndexOutOfBoundsException(); }
        StringTokenizer answers = new StringTokenizer(s);
        for(int i=0; i<bitString.size(); i++)
        {
            String currentToken = answers.nextToken();
            if(currentToken.equals("t") || currentToken.equals("T")) { bitString.set(i); }
            else if(currentToken.equals("f") || currentToken.equals("F")) { bitString.clear(i); }
        }
    }
    catch(IndexOutOfBoundsException ioob){System.out.println("Sorry bub, too many answers.");}
}

来自BitMap.java的相关构造函数(将传递的参数解析为BitSets,提供按位操作)

{{1}}

我为大量的代码道歉,但我认为更多的信息比不够好。

1 个答案:

答案 0 :(得分:1)

您没有初始化answers,因此它将以null开头。在recordResponse中,您在更新前打印answers,因此在输入第一个答案后,您打印null,然后您有"nullt/f -- t/f ..."

所以,你想要

private static String answers = "", userName;

和诊断,为了相关,最有可能在更新后进行:

if(currentAnswer.equals("t")||
   currentAnswer.equals("T")||
   currentAnswer.equals("f")||
   currentAnswer.equals("F"))
{
  answers += currentAnswer + " -- ";
  System.out.println("Answer: " + answers);
}