user3001857
user3001857

Reputation:

Randomly choosing elements from array without choosing an element twice

I have 2 arrays that are shuffled in the same way. Basically I want to remove the array elements once it's been asked to avoid getting duplicates being printed out:

How do I go about this, do I need to use an array list? I am completely lost.

/*
 * *** Random Test ***
 * Allows the user to generate a random test
 * from a bank of 10 questions.
 */

// import java libraries
import java.util.Scanner;
import java.util.Random;

public class randomTest {
public static void main(String args[]) {
    Scanner sc = new Scanner (System.in);


    // Question bank
    String[] qBank = new String[] {"What is the capital of England?", 
            "Which animal comes first in the English dictionary",
            "How many days are there in a leap year?"};

    // Answer bank
    String[] aBank = new String[] {"London", 
            "Aardvark", 
            "366"};

    // How many questions?
    System.out.println("How many questions would you like? (1-10)");
    int numQuestions = Integer.parseInt(sc.nextLine());
    int asked = 0;

    // Already Asked
    int[] alreadyAsked = new int[numQuestions];


    while (numQuestions != asked) {

        // Random question
        int idx = new Random().nextInt(qBank.length);
        String question = (qBank[idx]);
        System.out.println(question);


        System.out.println("Enter your awnswer: ");
        String myAnswer = sc.nextLine();
        asked ++;


        if (myAnswer.equals(aBank[idx])) {
            System.out.println("Correct answer!");
        } else {
            System.out.println("Wrong answer");
        }
    }
    }
}

Upvotes: 0

Views: 1275

Answers (3)

Tim B
Tim B

Reputation: 41188

Using an ArrayList<String> will be your best solution, then you can just do list.remove() to remove entries as they are used.

Collections.shuffle() will shuffle one list. What you probably actually want to do though is just choose one element at random from the list each time around the loop:

 List<String> questions = new ArrayList<>();
 questions.add("question 1");
 questions.add("question 2");
 etc
 List<String> answers = new ArrayList<>();
 answers.add("answer 1");
 etc

 Random random = new Random();

 while (questionsRemaining-- > 0) {
     int q = random.nextInt(questions.size());
     // ask question questions.get(q);
     // check against answer answers.get(q);

     // Removed used
     questions.remove(q);
     answers.remove(q);
 }

Really though you would be better off defining a Question object and having both Q and A inside that, then you don't have to maintain two lists and in the future you can add more data (like different scores for different questions, multiple answers, etc).

Upvotes: 1

Gorbles
Gorbles

Reputation: 1168

Use a 2D array and set the questions and answers together.

String[][] example = new String[2][10];
example[0, 0] = "What is the capital of England?";
example[1, 0] = "London";

You don't have to type it out the tedious way, it works with the array instantiation you have above (using { }).

Alternatively, an ArrayList<String[]>. Just set the String[] as {"question", "answer"} and add it to the ArrayList.

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136012

If you dont want duplicates use a Set. To remove duplicates from a List you can use Set s = new HashSet(list);

Upvotes: 0

Related Questions