0

I have a method that have to compare an integer with an Arraylist and if the number on the integer is on the arraylist, I recieve a false. If the integer number is not on the arraylist I recieve a true.

I need this, because I'm making a quiz game that have some questions on a BBDD and I can't show the same question twice. Also this questions are in three categories: E, I and A. I have to show five E questions, then five I questions and then five A questions.

So I have to show questions without repeat them, and also 5 of each category. I make this methods but I don't know why it repeats some questions.

The only thing works correctly is the category questions. I have always 5 E questions, then 5 I questions and at least 5 A questions.

The method that told me if the questions is on the ArrayList:

private boolean checkRandom()
{
    for(int i = 0; i < listRandom.size(); i++)
    {
        if(questionPosition == listRandom.get(i))
        {
            return false;
        }
    }

    return true;
}

I also try with "equals", but I have the same result.

The method that check if the question is in the correct category and is not showed before:

private void loadPhase() {

    if (numQuestion < 15)
    {
        r = new Random();
        questionPosition = r.nextInt(listQuestions.size());

        if(numQuestion < 5)
        {
            while(!listPhase.get(questionPosition).equals("E") && (!listPhase.get(questionPosition).equals("E")|| !checkRandom()))
            {
                r = new Random();
                questionPosition = r.nextInt(listQuestions.size());
            }
        }
        else if(numQuestion > 4 && numQuestion < 10)
        {
            while(!listPhase.get(questionPosition).equals("I") && (!listPhase.get(questionPosition).equals("I")|| !checkRandom()))
            {
                r = new Random();
                questionPosition = r.nextInt(listQuestions.size());
            }
        }
        else if(numQuestion > 9)
        {
            while(!listPhase.get(questionPosition).equals("A") && (!listPhase.get(questionPosition).equals("A")|| !checkRandom()))
            {
                r = new Random();
                questionPosition = r.nextInt(listQuestions.size());
            }
        }

        listRandom.add(questionPosition);

        header4questions.setText(listQuestions.get(questionPosition));

        answer1.setText(listAnswers.get(questionPosition * 4));
        answer2.setText(listAnswers.get((questionPosition * 4) + 1));
        answer3.setText(listAnswers.get((questionPosition * 4) + 2));
        answer4.setText(listAnswers.get((questionPosition * 4) + 3));

        numQuestion++;
    }
    else {
        Toast.makeText(getApplicationContext(), "You have finished the quiz!", Toast.LENGTH_SHORT).show();

        uploadPoints();
    }
}

So, what I have is all the questions on the correct category, but it repeat the questions and I don't know why.

Can anyone help me?

Thanks!

Imrik
  • 674
  • 2
  • 14
  • 32
  • 2
    You really need to debug this code to find out the error. – Suresh Atta Jul 05 '17 at 11:21
  • To pick `n` questions at random from a `List` without replacement, use `Collections.shuffle` to shuffle the list and then just take the first `n` items from it. – David Conrad Jul 05 '17 at 11:28
  • @DawoodibnKareem what do you mean with get(iny) is different from get(object)? is that my error? – Imrik Jul 05 '17 at 11:28
  • No, I'm entirely wrong, so I've unthumped the question. I'm going to have to study your question more carefully. – Dawood ibn Kareem Jul 05 '17 at 11:31
  • ok, I wait @DawoodibnKareem Thanks a lot! – Imrik Jul 05 '17 at 11:37
  • Change while condition with following: `while(!listPhase.get(questionPosition).equals("E") && checkRandom() && !listPhase.get(questionPosition).equals("E"))` – Shayan Pourvatan Jul 05 '17 at 11:48
  • @ShayanPourvatan with this, I have questions of category A before they have to appear. I need five E, five I and five A. Also it repeat the questions. It doesn't work. – Imrik Jul 05 '17 at 11:55

3 Answers3

0

Replace your return as below

private boolean checkRandom()
{
    for(int i = 0; i < listRandom.size(); i++)
    {
        if(questionPosition == listRandom.get(i))
        {
            return true;
        }
    }

    return false;
}
Niraj Sanghani
  • 1,493
  • 15
  • 23
  • I try. Wait! :) – Imrik Jul 05 '17 at 11:35
  • As in question said: _if the number on the integer is on the arraylist, I recieve a false_ so, it's fine the way it is now!! – Mohammad Zarei Jul 05 '17 at 11:36
  • As you see in the question, I recieve a false if the integer is on the arraylist, and in the other method, when I call checkRandom, I call it with that "!checkRandom()". The exclamation check is for this false value that I want to recieve if the integer is on the arraylist. I try with your answer and it doesn't work. – Imrik Jul 05 '17 at 11:48
  • I thing Best way is to use Collection class and use Binary search the Id to check if exist, will be more efficient – Niraj Sanghani Jul 05 '17 at 13:01
0

Although converting the ArrayList to a HashSet effectively removes duplicates, if you need to preserve insertion order, I'd rather suggest you use this variant

list is some List of Strings
Set < String > s = new LinkedHashSet < > (list);

Then, if you need to get back a List reference, you can use again the conversion constructor.

Answer reference: https://stackoverflow.com/a/204004/8252521

Answered by user: https://stackoverflow.com/users/27565/abahgat

  • I don't have duplicates, I mean... I have a random number that is compared with the ArrayList that have the questions, but the ArrayList doesn't have duplicates. – Imrik Jul 05 '17 at 12:10
0

I solve it! I have to change the while condition like this:

while(!listPhase.get(questionPosition).equals("E") || (listPhase.get(questionPosition).equals("E") && !checkRandom()))
Imrik
  • 674
  • 2
  • 14
  • 32