I'm trying to do a q & a program which will read a question, 4 choices. Then read the answer and compare it to the correct answer. Then increments the player score,
My problem is, I always get null when I run the program. Could you please help me determine what's wrong with my program. I also get java.lang.nullexception and index array out of bounds.
File structure looks like this:
q
a
b
c
d
ans
where q is the question a,b,c,d are the choices. And the ans is the correct answer which will be compared to the players answer.
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];
try {
FileReader fr;
fr = new FileReader (new File("F:\\qa.txt"));
BufferedReader br = new BufferedReader (fr);
while (br.readLine()!= null) {
for(int ox=0;ox<5;ox++){
q[ox]= new String();
a[ox]= new String();
b[ox]= new String();
c[ox]= new String();
d[ox]= new String();
q[ox] = br.readLine();
a[ox]=br.readLine();
b[ox]=br.readLine();
c[ox]=br.readLine();
d[ox]=br.readLine();
String strtemp=br.readLine();
ans[ox]=strtemp.charAt(0);
}
}
br.close();
} catch (Exception e) { e.printStackTrace();}
for(int yy=0;yy<90;yy++){
int ran= random(1,70);
System.out.println(q[ran] + "\n" + a[ran] + "\n" + b[ran] + "\n" + c[ran] + "\n" + d[ran] + "\n" + "\n" + "Answer: ");
String strans=x.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!");
}
}
Here's the code that I use for randomizing:
public int random(int min, int max){
int xx;
xx= (int) ( Math.random() * (max-min + 1))+ min;
return xx;
}