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.
You didn’t initialize
answers, so it will start out asnull. InrecordResponseyou print outanswersbefore 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: