0

I would like to add to my code possibility Random questions without repetition. By now I was able to do this: (from txt file Random value the question - answer) so it looks like my original code:

 public void value (){


    
        if(licznik>=number && licznik <= (2*number-1)){
            
            is = getResources().openRawResource(R.raw.poziom2pyt);
            is2 = getResources().openRawResource(R.raw.poziom2odp);
            
        
      
       
            else{
                is = getResources().openRawResource(R.raw.poziom1pyt);
                is2 = getResources().openRawResource(R.raw.poziom1odp);
                
            }
        
        
         Scanner questionScanner = new Scanner(is);
         Scanner answerScanner = new Scanner(is2); 

         
        ArrayList<String> answerList = new ArrayList<String>();
          ArrayList<String> questionList = new ArrayList<String>();

           try {
               while (answerScanner.hasNextLine() ) {
                   answerList.add(answerScanner.nextLine());  
                   questionList.add(questionScanner.nextLine()); 
               }
           } finally {
               answerScanner.close();  
               questionScanner.close();
           }  

           int nextInt = random.nextInt(questionList.size());

    
           
           String answerString = answerList.get(nextInt).replace("\uFEFF", "");
           String questionString = questionList.get(nextInt).replace("\uFEFF", "");
           yourAnswerString = answerString.substring(0);
           yourQuestionString = questionString.substring(2);
           shortform = questionString.substring(0,1);
           
     

       }

My question is this: How can I convert existing code to the Random answer questions- their value does not repeated example:

I have 10 questions:

1 Do you like cats?

2 Do you like dogs?

3 Do you like chamsters?

4 ...

5...

6..

7 Do you like rabbits?

8..

9..

10..

The first time the function is called value gets in response:

1.Do you like cats?

then the next time it is called it should be only 9 possibilities (2,3,4,5,6,7,8,9,10)

During the second call to get such value

7 Do you like rabbits?

then there are questions about the issue: 2,3,4,5,6,8,9,10

(Question No. 1 is not returned to the pool until they restart the application)

Community
  • 1
  • 1
Icero
  • 81
  • 11
  • I saw this answer but I need this on String values ( I have in my txt files value like: 1. aaaaa 2.bbbbb 3.ccccc etc. – Icero Mar 13 '15 at 14:03
  • You can get a random question by getting an index in [0, List.size]. Then remove the element ( List.remove(index) ). All index are updated when you do this ; you just have to pick another random into [0; List.size] – DavidL Mar 13 '15 at 14:08

0 Answers0