0

i have my quizz game reading a csv file to get the questions but i dont know how to randomize them for every time start the game i dont know if its possible with the code i have and i dont want to change to sql or sqllite because i already did this way and i want to finish like that, thats my code to read the file

import android.content.Context;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CsvFileReader {

    public ArrayList<Question> readFile(Context ctx){
        ArrayList<Question> questions = new ArrayList<>();
        InputStream inputStream = ctx.getResources().openRawResource(R.raw.question);
        CSVFile csvFile = new CSVFile(inputStream);
        List<String[]> scoreList = csvFile.read();

      for (int i=1;i<scoreList.size();i++)
        {
            String[] strings = scoreList.get(i);

            int questionId = 0;
            String question = "";
            int dificulty = 0;
            int correctAnswer = 0;
            String answer1 = "";
            String answer2 = "";
            String answer3 = "";
            String answer4 = "";

            int length = strings.length;
            if (length>0){

                try {
                    questionId = Integer.parseInt(strings[0]);
                }catch (Exception ex){

                }

            }
            if (length>1){

                question = strings[1];
            }

            if (length>2){
                try {
                    dificulty = Integer.parseInt(strings[2]);
                }catch (Exception ex){

                }

            }

            if (length>3){
            try {
                correctAnswer = Integer.parseInt(strings[3]);
            }catch (Exception ex){

            }
            }


            if (length>4){
                answer1 = strings[4];

            }

            if (length>5){
                answer2 = strings[5];
            }

            if (length>6){
                answer3 = strings[6];
            }

            if (length>7){
                answer4 = strings[7];
            }



            Question questionData = new Question(questionId,question,dificulty,correctAnswer,answer1,answer2,answer3,answer4);


            questions.add(questionData);
        }
      
        return  questions;
    }
}

i want every time i start the quizz need to randomize the questions and down there is the code from the file

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CSVFile {
    InputStream inputStream;

    public CSVFile(InputStream inputStream){
        this.inputStream = inputStream;
    }

    public List read(){
        List resultList = new ArrayList();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String csvLine;
            while ((csvLine = reader.readLine()) != null) {
                String[] row = csvLine.split(",");
                resultList.add(row);
            }
        }
        catch (IOException ex) {
            throw new RuntimeException("Error reading: "+ex);
        }
        finally {
            try {
                inputStream.close();
            }
            catch (IOException e) {
                throw new RuntimeException("Error while closing input stream: "+e);
            }
        }
        return resultList;
    }
}

if someone need more code to get in or whatever let me know i would appreciate the help.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • As an aside, inside `read` you should declare `resultList` as a `List`. In general [you shouldn't use raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Federico klez Culloca Jun 22 '21 at 11:05
  • It is definitely possible, a bit tricky. You could try to randomize questions by copying them in another column randomizing rows each time you start the game. Or your could take random rows during the game, but you also should find a way to bool the question as "Already Asked". Personally I would have chosen OOP and a JSON file for questions, it saves you a lot of string handling – Fabio R. Jun 22 '21 at 11:05

1 Answers1

0

You can use Collections.shuffle to randomize an arrayList. This function is in java.util.

Something like this:

import java.util.Collections;
...
public ArrayList<Question> readFile(Context ctx){
     ArrayList<Question> questions = new ArrayList<>();
     ...
     Collections.shuffle(questions);
     return questions;
}

This will return a randomize ArrayList

Dharman
  • 30,962
  • 25
  • 85
  • 135
AlvaroB
  • 68
  • 5