Home > Net >  creating exploitable .csv file for java program
creating exploitable .csv file for java program

Time:12-01

I'm both new to JAVA and .csv file so bear with me please! Here's my problem, i have this word file :

enter image description here

basically i have a theme (here Science) a difficulty level, a question number, a question, available answers and a right answer

I would like to be able to create a .csv file that i could then exploint in a java program, i'd like to store the different themes in an arraylist, the questions and their answers in another arraylist and so on. But i don't really know what's my best option since as you can see i have different formats of questions, it can be true or false, multiple choice question, and also just a question where the user would have to answer directly with a sentence. Which means that for example in that case i wouldnt have a "available answer" field.

Thanks for any help or suggestions

CodePudding user response:

The question is not totally clear to me. I am assuming you want to organize some data inside a CSV that later a Java program will parse in order to execute a quiz. I am also assuming that you are struggling with how the CSV file should be organized.

One solution

One CSV possibility would be (assuming you don't have any comma or semicolon inside your data) :

question_number,theme,difficulty,question,possible_answers,correct_answer
0,Sport,Medium,Who is current leader at formula 1 championship ?, 1. Hamilton;2. Verstappen,2. Verstappen
1,Science,Easy,What does the acronym LASER stands for ?,1. Light Amplification Stimulated by Emission of Radiation;2. Light Amplification Stimulated by Emission of Rigatoni,

For readability : Possible CSV example

As you saw the file is divided into different columns that are comma-separated. The possible_answers column includes itself a list of the possible answers which are semicolon-separated. This should be easily parsable with basic Java and be coherent with the implementation that you described.

This format would not allow the use of comma or semi-columns inside the data. In case you want to allow them you might want to escape the column values using double-quotes. This process is described by the RFC 4180 CSV standard. (The RFC 4180 standard has no official value regarding the CSV format itself but it is a commonly accepted description of it)

  1. Each field may or may not be enclosed in double-quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. For example:

    "aaa","bbb","ccc" CRLF zzz,yyy,xxx

  2. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

    "aaa","b CRLF bb","ccc" CRLF zzz,yyy,xxx

Alternative solution idea

Assuming the maximum number of "possible_answers" is knwon, an alternative solution would have been to include some columns

answer_1,answer_2,answer_3,answer_4

And to fill them only when necessary. This would avoid the trick of parsing a semi-column separated list of possible answers.

  • Related