2011-10-23 6 views
-3

答えのキーと各ユーザーの回答の配列の両方をビットセットとして保存するJavaコースの真偽テストを作成しています。その後、これらのBitSetは永続シーケンシャルバイナリファイルにシリアル化され、後で別のアプリケーションによってスコアリングされる可能性があります。BufferedReader.readLine()ファイルの末尾ではなく、最初にヌルを返します

request.readLine()への私の最初の呼び出しの奇妙な例外を除いて、すべてが完璧に機能します。私のsystem.in inputstreamreaderはユーザーの回答を取得します。何らかの理由で、EOFに達したかのように、入力された内容に関係なく最初の応答をnullに設定しています。

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から関連のあるコンストラクタ(解析はビット集合に引数を渡します、ビット演算を提供します)

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.");} 
} 

私は、コードの質量をお詫び申し上げますが、私はより多くの情報が十分ではないよりはましだ把握。

+1

「TEXT.TXTは」のように何を求めませんテキストエディタ? –

+0

非常に簡単です。それはちょうど25の質問です、1行につき1つの質問は疑問符で終わります。先頭または末尾に空白はありません。あなたが好きなら投稿することができます。このことは首にかかる痛みです。私はJavaを好きになったことがなく、彼らがJavaでプログラミングコースを教えているという事実が私を夢中にしている。私は主にPHPプログラマです。 – madhouse

答えて

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); 
} 
+0

それはそれだった。あなたはブロダです。私がそれを初期化するとすぐに、それは完全に働いた。 – madhouse

関連する問題