7 Replies - 855 Views - Last Post: 08 December 2011 - 06:33 PM Rate Topic: -----

#1 lexrst  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 11

problem with random array giving repeat questions

Posted 07 December 2011 - 02:13 PM

When I run the program it asks 10 questions randomly from the list. It repeats some of the questions though and can't figure out what to do. I have tried using collections and shuffle but no luck with that. I am a novice with arrays and haven't used the collections or shuffle until now so not sure if I was using it right. The program is for me to test my medical terminology and isn't for homework or production. Here is what I have, the program does work it just gives repeats.

import java.util.Random;
import java.util.Scanner;

public class Terminology
{
    
public static void main(String[] args)
{       
    String name = null;
    String input;       
    char repeat;       
      
do
{
    int count = 1; 
    do
    {
        Random generator = new Random();
        
        String[] terminology = {"gland", "joint", "life", "cancer", "heart", "head", "cerebrum", "urinary bladder",
        "cell", "skin", "electricity", "brain", "intestines", "red", "stomach", "Knowledge", "woman", "blood", "liver"};
        
        String[] answer = {"aden/o", "arthr/o", "bi/o", "carcin/o", "cardi/o", "cephal/o", "cerebr/o", "cyst/o", "cyt/o",
        "derm/o", "electr/o", "encephal/o", "enter/o", "erythr/o", "gastr/o", "gnos/o", "gynec/o", "hemat/o", "hepat/o"};
        
        int n = terminology.length;
        int r = generator.nextInt(n);
        String term = terminology[r];
        
        System.out.println("What is the combining form of " + term + "?");
        
        Scanner sc;        
        sc = new Scanner(System.in);
        name = sc.nextLine();
        
        if (answer[r].equalsIgnoreCase(name))
        {
            System.out.println("Thats right!" + "\n");            
        }
        else
        {
            System.out.println("Sorry thats wrong" + "\n" + ("The correct answer is " + answer[r] + "\n"));            
        }
        count++;
    }while (count <=10);
    
    System.out.println("Would you like to test again? " + "Enter Y for yes or N for no: ");
    
    Scanner keyboard = new Scanner(System.in);
    input = keyboard.next();
    repeat = input.charAt(0);
    
}while(repeat == 'Y' || repeat == 'y');
}
}



Thanks,
Kevin

Is This A Good Question/Topic? 0
  • +

Replies To: problem with random array giving repeat questions

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: problem with random array giving repeat questions

Posted 07 December 2011 - 03:05 PM

Look at Java Sets. A common way to avoid repeats in a random number generator is to generate the number; check to see if the set contains the number; if the number already exists, generate another; when a unique number is generated that is not already in the set; add it to the set and then use it in your program.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: problem with random array giving repeat questions

Posted 07 December 2011 - 07:51 PM

First create only ONE Random object and ONE Scanner don't create a new one at each loop iteration

Second the easiest way is to remove randomly from an ArrayList

http://www.dreaminco...snippet2992.htm

but it can be done with an array. Just have a count of the number of element in the array, remove randomly from it, replace the randomly selected one by the last one and start over

String[] question = {"a", "b", "c", "d"};
String[] answer = {"w", "x", "y", "z"};
int count = question.length;
Random ran = new Random();


do {
   int index = ran.nextInt(count);     // random based on number of element in the array
   String q = question[index];         // use that question   
   String a = answer[index];           // use that answer
   count--;                            // update count
   question[index] = question[count];  // move last to selected one
   answer[index] = answer[count];      // on both array
   ...
}



Happy coding
Was This Post Helpful? 2
  • +
  • -

#4 lexrst  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 11

Re: problem with random array giving repeat questions

Posted 07 December 2011 - 09:02 PM

Thanks pbl for the info, I went and changed things around and now it doesn't repeat the questions but the answers aren't matching up with the questions. If the question is "What is the combining form of gland?" and you type in aden/o which is right it will come back and say it is wrong and give a different random answer.

import java.util.Random;
import java.util.Scanner;

public class Terminology
{
    
public static void main(String[] args)
{       
    String name = null;
    String input;
    
    String[] question = {"gland", "joint", "life", "cancer", "heart", "head", "cerebrum", "urinary bladder",
        "cell", "skin", "electricity", "brain", "intestines", "red", "stomach", "Knowledge", "woman", "blood", "liver"};
        
        String[] answer = {"aden/o", "arthr/o", "bi/o", "carcin/o", "cardi/o", "cephal/o", "cerebr/o", "cyst/o", "cyt/o",
        "derm/o", "electr/o", "encephal/o", "enter/o", "erythr/o", "gastr/o", "gnos/o", "gynec/o", "hemat/o", "hepat/o"};
        
        int count = question.length;
        Random ran = new Random();
        int n = 1;
    do
    {        
        int index = ran.nextInt(count);
        String q = question[index];
        String a = answer[index];
        count--;
        question[index] = question[count];
        answer[index] = answer[count];
        
        System.out.println("What is the combining form of " + q + "?");
        
        Scanner sc;        
        sc = new Scanner(System.in);
        name = sc.nextLine();
        
        if (answer[index].equalsIgnoreCase(name))
        {
            System.out.println("Thats right!" + "\n");            
        }
        else
        {
            System.out.println("Sorry thats wrong" + "\n" + ("The correct answer is " + answer[index] + "\n"));            
        }
        n++;
    }while (n <=10);    
}
}



Thanks,
Kevin
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: problem with random array giving repeat questions

Posted 07 December 2011 - 09:18 PM

if (answer[index].equalsIgnoreCase(name))

if(a.equalsIgnoreCase(name))

not answer[index] that has been replaced by answer[count] :)
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: problem with random array giving repeat questions

Posted 07 December 2011 - 09:24 PM

Actually you can also do it that way

        int count = question.length;
        Random ran = new Random();
        int n = 1;
    do
    {        
        int index = ran.nextInt(count);
        
        System.out.println("What is the combining form of " + question[index] + "?");
        
        Scanner sc;        
        sc = new Scanner(System.in);
        name = sc.nextLine();
        
        if (answer[index].equalsIgnoreCase(name))
        {
            System.out.println("Thats right!" + "\n");            
        }
        else
        {
            System.out.println("Sorry thats wrong" + "\n" + ("The correct answer is " + answer[index] + "\n"));            
        }
        n++;
        count--;
        question[index] = question[count];
        answer[index] = answer[count];
    }while (n <=10);    
}
}


Was This Post Helpful? 1
  • +
  • -

#7 lexrst  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 11

Re: problem with random array giving repeat questions

Posted 07 December 2011 - 11:31 PM

got it now, I had looked at that and missed the conversion so it would be a and not index. Just so I understand the concept behind it, does the index hold the questions that get pulled from the array and then it gets read from the index?And if it is the same it dumps it and pulls another one? Thanks again really do appreciate the help.

Kevin
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,351
  • Joined: 27-December 08

Re: problem with random array giving repeat questions

Posted 08 December 2011 - 06:33 PM

Also, please avoid parallel arrays. Design a Question class that encapsulates the question and answer, then store a Question[]. My tutorial Moving Away From Parallel Arrays covers this in more detail.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1