Results 1 to 6 of 6
  1. #1
    Spinz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Question HELP! How to write simple code for multiple choice quizz with java!

    Hi, I am trying to do this for an assignment but have no idea how to implement it with Java.

    There is a text file with some multiple choice questions and answers, these represent flashcards, such that the Q and A1, A2, A3,..., Ak are enclosed between spaces (if there is more than one question).

    For example:

    What is the Capital of British Columbia
    Victoria // correct answer is always the first m/c
    Coquitlam
    Richmond
    Vancouver
    //space
    Another question
    with its answers
    //space
    etc

    I would like to store the Question, and consecutive answers into a string array until it reaches the space (indicating the end of the flashcard).
    However, since there are several questions with no specific amount of answers (thus the Akth answer), this array must be able to grow and shrink.

    I was thinking of the following:

    Java Code:
                   //create array
                   Array[] cards;
    
                   //create index for counting lines and another index for counting array slots
                   int arrayindex;
                   int textindex; 
    
    		while (scan.hasNext() == true) {
    			String lineOfText = scan.nextLine() ;
    			if (lineOfText.length() == 0) {
    				continue ; //until find a textline
                           
    
                            //somehow store text until arriving at space?!?!?!
                    
    			}
    Don't laugh guys, might not be much but I am really new to this so I'd greatly appreciate your help

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: HELP! How to write simple code for multiple choice quizz with java!

    Are you aware of ArrayList? Look it up in the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: HELP! How to write simple code for multiple choice quizz with java!

    Also, never compare booleans expressions to boolean literals. It's simply not needed, and in the case of variables it's error-prone.
    Java Code:
    // while (scan.hasNext() == true) {
    while (scan.hasNext()) {
    Use the negation operator ! to test that a boolean expression or variable is false.
    Java Code:
    // if (something == false) {
    if (!something) {
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: HELP! How to write simple code for multiple choice quizz with java!

    Java Code:
    import java.util.Scanner 
    import java.util.ArrayList;
    import java.util.HashMap;
    
    class YourClassName{
    	public static void main(String args[]){
    		// you read the file
    		
            /* Now i believe that HashMap will be a really good choice because we have a connection
    		 * in Questions and the answers and the HashMap will have Strings and an ArrayList of Strings
    		 * in key What is the Capital of British Columbia the values will be ["Victoria ","Coquitlam","Richmond",...]
    		 */
    
    		HashMap<String,ArrayList<String>> myHashMap = new HashMap<String,ArrayList<String>>();
    		ArrayList<String> correctAnswer = new ArrayList<String>(); // will keeps the correct answer of each question
    		while(scan.hasNext()){
    			String question = scan.nextLine();
    			String line = scan.nextLine();
    			ArrayList<String> answers = new ArrayList<String>();
    			while(line.length!=0){ // if length == 0 that means we found space!
    				answers.add(line);
    				line = scan.nextLine();
    			}
    			myHashMap.put(question,answers);  // we put all the question with the answers in HashMap
    		}
    
    
    		/* when all this ends that will mean we have read all the file and we have save all the data in
    		 * a HashMap. Keys are the Questions and values of the HasHap are the answers 
    		 * now if you know the correct answers of each questions save them in correctAnswer ArrayList just to have them saved ;)
    		 * and now do whatever you want to do with HashMap
    		 */
    	}
    }

    hmm my code is in blue.. stange.. anyway.. if you know about HashMap it will be very easy to understand what is going on.. else you should take a look at HashMap first or to try another way.. good luck!
    Last edited by ShadowWalker; 07-07-2013 at 06:01 PM.

  5. #5
    Spinz is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: HELP! How to write simple code for multiple choice quizz with java!

    Your reply really helped! I didn't think of the hashmap, also because the prof hasn't really covered it, but it is a good way to implement the quiz. Thanks!

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: HELP! How to write simple code for multiple choice quizz with java!

    hmm if your Professor hasn't covered it yet maybe you should try onother way more complex but also correct ..

    Java Code:
    /** Let's think a different way.. we can create an ArrayList<String> which will keeps the questions
    *   and a second ArrayList<String> for the answers but now we should do something to split the answers 
    *   of each questions.. we can do that with a "," or something like that inside the ArrayList<String> 
    *   of answers for e.x in question What is the Capital of British Columbia the ArrayList of aswers will have
    *   ["Coquitlam","Richmond","Vancouver",",",....]
    */
    
    // let's start ! (inside your class)
    	public static void main(String args[]){
    		... // you read your file 
    		ArrayList<String> questions = new ArrayList<String>();
    		ArrayList<String> answers = new ArrayList<String>();\
    		while(scan.hasNext()){
    			String questionLine = scan.nextLine();
    			String line = scan.nextLine();
    			while(line.length == 0)){ // again if we found space!
    				answers.add(line);
    				line = scan.nextLine();
    			}
    			// when the while loop finished we have found space so we 
    			// add the question in questions ArrayList and we add a ";" or whatever symbol you choose 
    			// inside the answers ArrayList
    			questions.add(questionLine);
    			answers.add(";");
    			// so again we go in first while loop we read onother question if scan.hasNext and we do the
    			// same things!! ;) 
    		}
    		// when the first while loop ends we will have two ArrayList like that:
    		// questions = ["Who is the most Beautiful person on world","What is my Favourite food, ..."]
    		// answers = ["ShadowWalker","You","Moderators",";","apples","banana","none",";",....]
    		// and now do whatever you want ;)
    	}
    actually is not complex ;) but hashMap is more clear
    Last edited by ShadowWalker; 07-09-2013 at 03:12 PM.

Similar Threads

  1. Help with multiple choice questions :)
    By javalikeaboss in forum New To Java
    Replies: 8
    Last Post: 05-07-2012, 05:07 PM
  2. Replies: 4
    Last Post: 01-20-2012, 09:43 AM
  3. joptionpane quizz , improve my code ....
    By pipikrk in forum New To Java
    Replies: 6
    Last Post: 08-30-2011, 01:04 AM
  4. Replies: 3
    Last Post: 12-12-2008, 12:12 PM
  5. Need help with T/F and Multiple Choice
    By sayso36 in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 04:39 PM

Posting Permissions

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