randomize text file read in java
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: randomize text file read in java

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    randomize text file read in java

    I am trying to read a text file in java, basically a set of questions. With 4 choices and 1 answer. The structure looks like this:
    question
    option a
    option b
    option c
    option d
    answer


    I have no trouble 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<26;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();}


    }

    public static int random(int min, int max){
    int xx;
    xx= (int) ( Math.random() * (max-min + 1))+ min;
    return xx;
    }


    }



    The code above is predictable. The for loop just increments. And it displays the questions based on order.
    What I want to do is to be able to randomize through the text file, but still maintaining 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!");
    }


    And this is the method that I use for randomizing:

    public static int random(int min, int max){
    int xx;
    xx= (int) ( Math.random() * (max-min + 1))+ min;
    return xx;
    }


    There's the possibility that I get a null. What can you recommend that I would do so that I get no null when trying to randomize the questions?
    Can you see anything else that is wrong with my program?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: randomize text file read in java

    Please use code tags when posting code.

    Rather than maintaining all those arrays you should have a class that encapsulates 1 question, its possible answers and the actual answer.

    Then create an instance of this class for each question, its options and answer as you read them in. You can store the question objects in an ArrayList so you don't need to worry about how many you are reading in (ArrayLists grow to whatever size you need) and then you can use the Collections shuffle() method to randomise the list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,429

    Re: randomize text file read in java

    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center