Randomizing text file read in Java -
i trying read text file in java, set of questions. 4 choices , 1 answer. structure looks this:
question
option a
option b
option c
option d
answer
i have no trouble reading way:
public class rar{ public static string[] q=new string[50]; public static string[] a=new string[50]; public static string[] b=new string[50]; public static string[] c=new string[50]; public static string[] d=new string[50]; public static char[] ans=new char[50]; public static scanner sr= new scanner(system.in); public static void main(string args[]){ int score=0; try { filereader fr; fr = new filereader (new file("f:\\questions.txt")); bufferedreader br = new bufferedreader (fr); int ar=0; for(ar=0;ar<2;ar++){ q[ar]=br.readline(); a[ar]=br.readline(); b[ar]=br.readline(); c[ar]=br.readline(); d[ar]=br.readline(); string tempo=br.readline(); ans[ar]=tempo.charat(0); system.out.println(q[ar]); system.out.println(a[ar]); system.out.println(b[ar]); system.out.println(c[ar]); system.out.println(d[ar]); system.out.println("answer: "); string strans=sr.nextline(); char y=strans.charat(0); if(y==ans[ar]){ system.out.println("check!"); score++; system.out.println("score:" + score); }else{ system.out.println("wrong!"); } } br.close(); } catch (exception e) { e.printstacktrace();} } }
the code above predictable. loop increments. , displays questions based on order.
what want able randomize through text file, still maintaining same structure. (q, a, b, c, d, ans). when try this:
int ran= random(1,25); system.out.println(q[ran]); system.out.println(a[ran]); system.out.println(b[ran]); system.out.println(c[ran]); system.out.println(d[ran]); system.out.println("answer: "); string strans=sr.nextline(); char y=strans.charat(0); if(y==ans[ran]){ system.out.println("check!"); score++; system.out.println("score:" + score); }else{ system.out.println("wrong!"); }
and method use randomizing:
public static int random(int min, int max){ int xx; xx= (int) ( math.random() * (max-min + 1))+ min; return xx; }
there's possibility null. can recommend no null when trying randomize questions?
can see else wrong program?
you use kind of magic numbers, numbers in code don't make sense.
public static string[] q=new string[50]; //why make array hold 50 questions? //... for(ar=0;ar<2;ar++){ //why read 2 questions? //... int ran= random(1,25); //why take 1 of 25 questions? system.out.println(q[ran]);
these should same number, right? if have 25 questions, should have room 25, read 25 , use 25.
how fix this:
1 make constant
public final static int number_of_questions = 25;
then use when making array, reading questions , when taking random one:
public static string[] q=new string[number_of_questions]; for(ar=0;ar<number_of_questions;ar++){ int ran= random(1,number_of_questions);
2 use q.length
public static string[] q=new string[number_of_questions]; for(ar=0;ar<q.length;ar++){ int ran= random(1,q.length);
3 use list / collection
public static list<string> q=new list<string>(); for(ar=0;ar<q.size();ar++){ int ran= random(1,q.size());
option 3 best choice, java afterall. see mike's answer more detail in making more java.
Comments
Post a Comment