Results 1 to 10 of 10
  1. #1
    overlordjebus is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Printing an Array

    I have a problem with a program I'm making for school. I am making a physics test in an applet and I need a way to list the score at the end of a test.

    When a user answers a question, whether the question is right or wrong is put into a boolean array (Correct being true and wrong being false)

    Once the user finishes the test, a page is shown that gives them there score out of the total score.

    What I want this screen to also show is a list of the which questions they got right and which they got wrong, something simple like this:

    Question 1: Wrong
    Question 2: Correct
    Question 3: Correct

    and so on, but I just cannot think of any way of doing this, since the user chooses the amount of questions they get asked, the list is going to be a different size everytime, so I cant just use a bunch of JTextfields or JLabels.

    I really just cant think of a way I can do this, I have only been programming java for a year, and this is the first time in dealing with Applets so I don't know all the little tricks and stuff, but I just simply can't think of a way to do it, anyone got any ideas?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Use a map keep the information, question and the answer. Possible to change the size dynamically. Read more about Collextions in Java.

  3. #3
    overlordjebus is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the help. I actually found another way of doing it, by just adding a new line into a string with a recursive algorithm, lets see how this works out!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure I'd use recursion for this. I'd probably display the results in a JTable or if you want it even simpler, a JList.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by overlordjebus View Post
    Thanks for the help. I actually found another way of doing it, by just adding a new line into a string with a recursive algorithm, lets see how this works out!
    You mean that you used additional consecutive line in the array?

  6. #6
    overlordjebus is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    This is what I did

    Java Code:
    while(X < QuestionNo)
            {
                if(Score[X] == true)
                {
                    AddingScoreString = ("Question" + (X + 1) + ": Correct" + NewLine);
                }
                else if(Score[X] == false)
                {
                    AddingScoreString = ("Question" + (X + 1) + ": Wrong" + NewLine);
                }
                
                QuestionScoresString = (QuestionScoresString + AddingScoreString);
                
                X = X + 1;
                
            }
    Then I just put QuestionScoresString in a JTextarea and it prints fine.
    Last edited by Eranga; 04-13-2010 at 04:44 PM. Reason: added code tags

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Regarding your code, what NewLine stand for? New line character?

    And also you can write the following code segment,

    Java Code:
    QuestionScoresString = (QuestionScoresString + AddingScoreString);
    X = X + 1;
    as,
    Java Code:
    QuestionScoresString += AddingScoreString;
    X += 1;

  9. #9
    overlordjebus is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Yes the Newline is a new line character

    Sorry, I'll remember to use code tags next time :)

    And thanks, I didn't know about the += thing, thanks :)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Problem printing an array
    By denial in forum New To Java
    Replies: 3
    Last Post: 11-01-2009, 11:09 PM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 10:12 AM
  3. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 09:57 PM
  4. printing array
    By mayhewj7 in forum New To Java
    Replies: 6
    Last Post: 02-12-2009, 03:01 PM
  5. Printing Byte Array
    By suchismitasuchi in forum New To Java
    Replies: 3
    Last Post: 01-19-2009, 11:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •