Randomize text files read in Java
I try to read a text file in Java, which is basically a set of problems There are four choices and one answer The structure is as follows:
I have no problem reading it this 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 above code is predictable The for loop is only incremented It will display the problem according to the order
What I want to do is to be able to randomize text files, but still maintain the same structure (q,a,b,c,d,ans). But when I try to do 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!"); }
This is my method for randomization:
public static int random(int min,int max){ int xx; xx= (int) ( Math.random() * (max-min + 1))+ min; return xx; }
I might get a null Can you suggest that I do this so that there are no null values when trying to randomize the problem?
Can you see what's wrong with my program?
Solution
You used all types of magic numbers in your code. These numbers don't make much sense
public static String[] q=new String[50]; //why make an array to hold 50 questions? //... for(ar=0;ar<2;ar++){ //why read 2 questions? //... int ran= random(1,25); //why take one of 25 questions? System.out.println(q[ran]);
These should be the same numbers, right? If we have 25 questions, we should have 25 spaces, 25 reads and 25 uses
How to solve this problem:
1 remain unchanged
public final static int NUMBER_OF_QUESTIONS = 25;
Then use it when making the array, read the questions, and when shooting at random:
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 / set
public static List<String> q=new List<String>(); for(ar=0;ar<q.size();ar++){ int ran= random(1,q.size());
Option 3 would be the best choice, which is Java after all For more details, see Mike's answer