BufferedReader.readLine() 在文件开头而不是结尾处返回 null
我正在为 Java 课程创建一个真/假测试,该课程将答案键和每个用户各自的答案数组存储为 BitSet。然后,这些位集被序列化为持久的顺序二进制文件,以便稍后可以由另一个应用程序对它们进行评分。
一切都很完美,除了我第一次调用 request.readLine()
时有一个奇怪的例外,这是我的 system.in
inputstreamreader,用于检索用户的答案。由于某种原因,无论输入什么内容,它都会将第一个答案设置为 null,就好像它已达到 EOF 一样。
CreateTest.java(从 txt 文件读取/显示问题,收集用户答案,将其存储在 .bin 文件中)
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();
}
BitMap.java 中的相关构造函数(将传递的参数解析为 BitSets,提供按位运算)
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.");}
}
我对大量代码表示歉意,但我认为更多信息总比不够好。
I'm creating a true/false test for a Java course that stores both an answer key and each user's respective array of answers as BitSets. These BitSets are then serialized into persistent sequential binary files so that they may be scored later by another application.
Everything works perfectly with the strange exception of my first call to request.readLine()
, which is my system.in
inputstreamreader that retrieves the user's answer. For some reason, it's setting the first answer to null regardless of what's entered, as if it had hit EOF.
CreateTest.java (reads/displays questions from txt file, collects user answers, stores them in .bin file)
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();
}
Pertinent constructor from BitMap.java (parses passed arguments into BitSets, provides bitwise operations)
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.");}
}
I apologize for the mass of code, but I figure more info's better than not enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有初始化
answers
,因此它将以null
开始。在recordResponse
中,您在更新之前打印出answers
,因此在输入第一个答案后,您将打印null
,然后就得到了” nullt/f -- t/f ..."
。因此,您想要的
诊断为了相关,很可能应该在更新之后进行:
You didn't initialize
answers
, so it will start out asnull
. InrecordResponse
you print outanswers
before the update, so after the first answer is entered you printnull
, and then you have"nullt/f -- t/f ..."
.So, you want
and the diagnostic, in order to be relevant, should most likely go after the update: