Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6737449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:15:01+00:00 2026-05-26T11:15:01+00:00

I’m creating a true/false test for a Java course that stores both an answer

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:15:01+00:00Added an answer on May 26, 2026 at 11:15 am

    You didn’t initialize answers, so it will start out as null. In recordResponse you print out answers before the update, so after the first answer is entered you print null, and then you have "nullt/f -- t/f ...".

    So, you want

    private static String answers = "", userName;
    

    and the diagnostic, in order to be relevant, should most likely go after the update:

    if(currentAnswer.equals("t")||
       currentAnswer.equals("T")||
       currentAnswer.equals("f")||
       currentAnswer.equals("F"))
    {
      answers += currentAnswer + " -- ";
      System.out.println("Answer: " + answers);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.