Hey!I have to make a Quiz game using arrays in java...Now,i have to create a random function such that the questions appear randomly n without any repetition?plz help...
Here's the code:

import java.util.*;

public class KBC
{
	String questions[];
	String options[][];
	int answers[];
	long money[];
	int history[];

	public KBC()
	{
		questions=new String[]{
			"What is the capital of Australia?",
			"Where is Fort William located ?",
			"Name this Indian Tennis player who has turned Hollywood filmmaker?",
			"On which riverbank is Goa located?",
 			"Which union ministry is the nodal agency to issue passports in India ?",
 			"In Harry Potter who plays Ron Weasley in the movie?",
			"The movies 'Hum Tum', 'Parineeta', 'Salaam Namaste', 'Being Cyrus', 'Omkara' and 'Eklavya' proved that this star had 'come of age'. Which one of the following was this star?",
			"Which country won the World Cup in 1999? ",
			"How many alphabets are in Gurmukhi ?",
			" In the wild, which animal is found only in China?",
			"In the epic Ramayana, which bird tried to prevent Ravana from carrying Sita away?",
			"Who is the Author of 'The Three Musketeers'?",
			"'.BAK' extension refers usually to what kind of file?",
			"Which of the following is known as 'diamond city' of India?",
			"On playing a dice which number is directly opposite to  1 ?"
	};
		options=new String[][]{
			{"1.Canberra","2.Melbourne","3.Brisbane","4.Sydney"},
			{"1.Chennai","2.Kolkata","3.Goa","4.Mysore"},
			{"1.Leander Paes","2.Mahesh Bhupathi","3.Vijay Amritraj","4.Ashok Amritraj"},
			{"1.Ganga","2.Mandovi","3.Gomati","4.Sabarmati"},
			{"1.External Affairs","2.Home Affairs","3.Defense Ministry","4.Tourism & Culture"},
			{"1.Macaulay Culkin","2.Robin Pinter","3.Robert Pattison","4.Rupert Grint"},
			{"1.Vidya Balan","2.Raima Sen","3.Sanjay Dutt","4.Saif Ali Khan"},
			{"1.Australia","2.South Africa","3.Pakistan","4.England"},
			{"1.35(Thirty-five)","2.36(Thirty-six)","3.37(Thirty-seven)","4.26(Twenty-six)"},
			{"1.Tiger","2.Asian Elephant","3.Giant Panda","4.Crocodile"},
			{"1.Vibhishan","2.Jatayu","3.Garuda","4.Bhulinga"},
			{"1.William Shakespeare","2.Alexandre Dumas","3.Robert L.B.Stevenson","4.Charles Dickens"},
			{"1.Backup file","2.Audio file","3.System file","4.MS Encarta document"},
			{"1.Ahmedabad","2.Mumbai","3.Surat","4.Goa"},
			{"1.Four","2.Two","3.Five","4.Six"},
	};
		answers=new int[]{1,2,3,2,1,4,4,1,1,3,2,2,1,3,4};

		money=new long[]{1000,3000,5000,10000,20000,50000,100000,160000,320000,640000,1250000,2500000,5000000,10000000,20000000};

		history=new int[15];

	}
	public int getRandomQuestion()

	{

		int i=(int)(Math.random()*15);
		return i;
	}
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		KBC k=new KBC();
		System.out.println("********************************************************************************");
		System.out.println("                     ::     ::    ::::::::     ::::::::");
		System.out.println("                     ::   ::       ::   ::     ::                         ");
		System.out.println("                     :: ::         ::   ::     ::                         ");
		System.out.println("                     :::::         :::::::     ::                          ");
		System.out.println("                     :: ::         ::   ::     ::                       ");
		System.out.println("                     ::   ::       ::   ::     ::                       ");
		System.out.println("                     ::     ::    ::::::::     ::::::::                       ");
		System.out.println("********************************************************************************");
		System.out.println("   ");
		int choice=0;

		for(int i=0;i!=k.questions.length;i++)
		{
			int idx=k.getRandomQuestion();
			System.out.println(k.questions[idx]);
			System.out.println(k.options[idx][0]);
			System.out.println(k.options[idx][1]);
			System.out.println(k.options[idx][2]);
			System.out.println(k.options[idx][3]);
			System.out.println("   ");
			System.out.println("PRESS 1,2,3 OR 4 : ");


			choice=Integer.parseInt(sc.nextLine());

			if(choice==k.answers[idx])
			{
				System.out.println("CONGRATS!!CORRECT ANSWER...YOU HAVE WON RS." +k.money[i]);
				System.out.println("   ");
				System.out.println("   ");
			}
			else
			{
				System.out.println("WRONG ANSWER!GAME OVER!");
				System.out.println("   ");
				System.out.println("   ");
				break;
			}
		}
	}
}

Create a List of Integers (which are the indexes to your questions) and use the random class to produce an int from zero to list.size() then get the Integer at that index from the list (and delete it) and then index into your question array using this Integer. Once the list is empty, recreate it.

I didnt get you...can u explain it via code?

IOW can I do it for you? No.

Create a list of Integers with

ArrayList<Integer> someVarName = new ArrayList<Integer>()

Then, for each item in your "question" array" add it's index as an Integer to that arraylist

Get a random number with

Random r = new Random();  // as an instance variable
...
int someIndex = r.nextInt(someVarName.size())

Then retreive the Integer at "someIndex" from the ArrayList with the get(int) method, then remove that "someIndex" from the list with remove(int). The Integer you retreived from the list is your index into questions and answers array.

Still..its not working!In the following function
public int getRandomQuestion() { int i=(int)(Math.random()*15); return i; }
If I store the value of i in the variable history and then give if-else conditions like if the particular question is not stored in 'history',then it can return that question on screen..but m unable to portray this..kindly guide me by explaining through code in the above functioon...
Actually,I m new to java..Its jst been 4 days I hav started..I hav to submit this assignment today only before 6pm....so plz help me...plzz...I wud really be thankful to u...plzz sir..

Show your complete code and provide all error/compiler messages (complete stack traces) and describe (briefly but completely) the difference between the expected and the actual output.

k.thnx.
hers's the code:

import java.util.*;

public class KBC
{
	String questions[];
	String options[][];
	int answers[];
	long money[];
	int history[];

	public KBC()
	{
		questions=new String[]{
			"Ques. What is the capital of Australia?",
			"Ques. Where is Fort William located ?",
			"Ques. Name this Indian Tennis player who has turned Hollywood filmmaker?",
			"Ques. On which riverbank is Goa located?",
 			"Ques. Which union ministry is the nodal agency to issue passports in India ?",
 			"Ques. In Harry Potter who plays Ron Weasley in the movie?",
			"Ques. The movies 'Hum Tum', 'Parineeta', 'Salaam Namaste', 'Being Cyrus', 'Omkara' and 'Eklavya' proved that this star had 'come of age'. Which one of the following was this star?",
			"Ques. Which country won the World Cup in 1999? ",
			"Ques. How many alphabets are in Gurmukhi ?",
			"Ques. In the wild, which animal is found only in China?",
			"Ques. In the epic Ramayana, which bird tried to prevent Ravana from carrying Sita away?",
			"Ques. Who is the Author of 'The Three Musketeers'?",
			"Ques. '.BAK' extension refers usually to what kind of file?",
			"Ques. Which of the following is known as 'diamond city' of India?",
			"Ques. On playing a dice which number is directly opposite to  1 ?"
	};
		options=new String[][]{
			{"1.Canberra","2.Melbourne","3.Brisbane","4.Sydney"},
			{"1.Chennai","2.Kolkata","3.Goa","4.Mysore"},
			{"1.Leander Paes","2.Mahesh Bhupathi","3.Vijay Amritraj","4.Ashok Amritraj"},
			{"1.Ganga","2.Mandovi","3.Gomati","4.Sabarmati"},
			{"1.External Affairs","2.Home Affairs","3.Defense Ministry","4.Tourism & Culture"},
			{"1.Macaulay Culkin","2.Robin Pinter","3.Robert Pattison","4.Rupert Grint"},
			{"1.Vidya Balan","2.Raima Sen","3.Sanjay Dutt","4.Saif Ali Khan"},
			{"1.Australia","2.South Africa","3.Pakistan","4.England"},
			{"1.35(Thirty-five)","2.36(Thirty-six)","3.37(Thirty-seven)","4.26(Twenty-six)"},
			{"1.Tiger","2.Asian Elephant","3.Giant Panda","4.Crocodile"},
			{"1.Vibhishan","2.Jatayu","3.Garuda","4.Bhulinga"},
			{"1.William Shakespeare","2.Alexandre Dumas","3.Robert L.B.Stevenson","4.Charles Dickens"},
			{"1.Backup file","2.Audio file","3.System file","4.MS Encarta document"},
			{"1.Ahmedabad","2.Mumbai","3.Surat","4.Goa"},
			{"1.Four","2.Two","3.Five","4.Six"},
	};
		answers=new int[]{1,2,3,2,1,4,4,1,1,3,2,2,1,3,4};

		money=new long[]{1000,3000,5000,10000,20000,50000,100000,160000,320000,640000,1250000,2500000,5000000,10000000,20000000};

		history=new int[15];

	}
	public int getRandomQuestion()

	{

		int i=(int)(Math.random()*15);
		return i;
		history[]=i;
		if(history!=i)
		return i;
	}
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		KBC k=new KBC();
		System.out.println("********************************************************************************");
		System.out.println("                     ::     ::    ::::::::     ::::::::");
		System.out.println("                     ::   ::       ::   ::     ::                         ");
		System.out.println("                     :: ::         ::   ::     ::                         ");
		System.out.println("                     :::::         :::::::     ::                          ");
		System.out.println("                     :: ::         ::   ::     ::                       ");
		System.out.println("                     ::   ::       ::   ::     ::                       ");
		System.out.println("                     ::     ::    ::::::::     ::::::::                       ");
		System.out.println("********************************************************************************");
		System.out.println("   ");
		int choice=0;

		for(int i=0;i!=k.questions.length;i++)
		{
			int idx=k.getRandomQuestion();
			System.out.println(k.questions[idx]);
			System.out.println(k.options[idx][0]);
			System.out.println(k.options[idx][1]);
			System.out.println(k.options[idx][2]);
			System.out.println(k.options[idx][3]);
			System.out.println("   ");
			System.out.println("PRESS 1,2,3 OR 4 : ");


			choice=Integer.parseInt(sc.nextLine());

			if(choice==k.answers[idx])
			{
				System.out.println("CONGRATS!!CORRECT ANSWER...YOU HAVE WON RS." +k.money[i]);
				System.out.println("   ");
				System.out.println("   ");
			}
			else
			{
				System.out.println("WRONG ANSWER!GAME OVER!");
				System.out.println("   ");
				System.out.println("   ");
				break;
			}
		}
	}
}

ERRORS:

C:\Users\AMAN\Desktop\NIIT\KBC.java:60: not a statement
history[]=i;
^
C:\Users\AMAN\Desktop\NIIT\KBC.java:60: ';' expected
history[]=i;
^
C:\Users\AMAN\Desktop\NIIT\KBC.java:60: not a statement
history[]=i;
^
3 errors

I want to store the random numbers generated in variable history....and then check if that particular question number has not appeared in variable history, then it can display that question..but I m unable to do it...plzz help...

I don't see anything to do with ArrayList there so I guess you don't want to follow my advice.

The other thing is, and an int parameter to your "getRandomQuestion" method to use as the index into the history array, both for the assignment and the check that you are doing in that method.

Also, using an array, you are going to have to check all indexes of the history array from 0 to the passed in parameter to make sure it differs from the number you just generated. Which means, of course, that the more numbers you've generated the longer it will take to generate a "unique" one. Using the ArrayList will avoid this.

k..but m nt able to do...sir plz...would u plz correct my code..??Im new to java..
I wud really b thankful to u...thnku

Regards,
Simran suri

commented: I know you're "new to java" and you'll [i]remain[/i] "new to java" if you don't bother to learn it. -2

And how would you learn anything if I did your work? You are in the course to learn something, not just to hand in finished work that you don't understand. It may facilityte a passing grade today, but tomorrows lesson, which will build off of todays, is that much harder.

Plz sir....I beg u...plz sir...plz help me...

I am helping you.

thnku so mch....thnku sir...
m really grateful to you...

I am waiting for ur reply...sir..plz help...

I am waiting on your attempt at implementing my last advice.

Sir,
I tried my best to do wat u adviced..her's the code...still an error is occuring..Is this wat m supposed to do..kindly guide me...

import java.util.*;


public class KB

{

	String questions[];
	String options[][];
	int answers[];
	long money[];
	int history[];
	int ArrayList[];

	public KB()
	{
		questions=new String[]{
			"Ques. What is the capital of Australia?",
			"Ques. Where is Fort William located ?",
			"Ques. Name this Indian Tennis player who has turned Hollywood filmmaker?",
			"Ques. On which riverbank is Goa located?",
 			"Ques. Which union ministry is the nodal agency to issue passports in India ?",
 			"Ques. In Harry Potter who plays Ron Weasley in the movie?",
			"Ques. The movies 'Hum Tum', 'Parineeta', 'Salaam Namaste', 'Being Cyrus', 'Omkara' and 'Eklavya' proved that this star had 'come of age'. Which one of the following was this star?",
			"Ques. Which country won the World Cup in 1999? ",
			"Ques. How many alphabets are in Gurmukhi ?",
			"Ques. In the wild, which animal is found only in China?",
			"Ques. In the epic Ramayana, which bird tried to prevent Ravana from carrying Sita away?",
			"Ques. Who is the Author of 'The Three Musketeers'?",
			"Ques. '.BAK' extension refers usually to what kind of file?",
			"Ques. Which of the following is known as 'diamond city' of India?",
			"Ques. On playing a dice which number is directly opposite to  1 ?"
	};
		options=new String[][]{
			{"1.Canberra","2.Melbourne","3.Brisbane","4.Sydney"},
			{"1.Chennai","2.Kolkata","3.Goa","4.Mysore"},
			{"1.Leander Paes","2.Mahesh Bhupathi","3.Vijay Amritraj","4.Ashok Amritraj"},
			{"1.Ganga","2.Mandovi","3.Gomati","4.Sabarmati"},
			{"1.External Affairs","2.Home Affairs","3.Defense Ministry","4.Tourism & Culture"},
			{"1.Macaulay Culkin","2.Robin Pinter","3.Robert Pattison","4.Rupert Grint"},
			{"1.Vidya Balan","2.Raima Sen","3.Sanjay Dutt","4.Saif Ali Khan"},
			{"1.Australia","2.South Africa","3.Pakistan","4.England"},
			{"1.35(Thirty-five)","2.36(Thirty-six)","3.37(Thirty-seven)","4.26(Twenty-six)"},
			{"1.Tiger","2.Asian Elephant","3.Giant Panda","4.Crocodile"},
			{"1.Vibhishan","2.Jatayu","3.Garuda","4.Bhulinga"},
			{"1.William Shakespeare","2.Alexandre Dumas","3.Robert L.B.Stevenson","4.Charles Dickens"},
			{"1.Backup file","2.Audio file","3.System file","4.MS Encarta document"},
			{"1.Ahmedabad","2.Mumbai","3.Surat","4.Goa"},
			{"1.Four","2.Two","3.Five","4.Six"},
	};
		answers=new int[]{1,2,3,2,1,4,4,1,1,3,2,2,1,3,4};

		money=new long[]{1000,3000,5000,10000,20000,50000,100000,160000,320000,640000,1250000,2500000,5000000,10000000,20000000};

		history=new int[15];

	}
	public int getRandomQuestion()

	{
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i=0;i!=questions.length;i++)
			{
				Arraylist=questions[i];
			}


		Random r = new Random();
		int i = r.nextInt(list.size());
		list.get((int)i);
		list.remove((int)i);






	}
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		KB k=new KB();
		System.out.println("********************************************************************************");
		System.out.println("                     ::     ::    ::::::::     ::::::::");
		System.out.println("                     ::   ::       ::   ::     ::                         ");
		System.out.println("                     :: ::         ::   ::     ::                         ");
		System.out.println("                     :::::         :::::::     ::                          ");
		System.out.println("                     :: ::         ::   ::     ::                       ");
		System.out.println("                     ::   ::       ::   ::     ::                       ");
		System.out.println("                     ::     ::    ::::::::     ::::::::                       ");
		System.out.println("********************************************************************************");
		System.out.println("   ");
		int choice=0;

		for(int i=0;i!=k.questions.length;i++)
		{
			int idx=k.getRandomQuestion();
			System.out.println(k.questions[idx]);
			System.out.println(k.options[idx][0]);
			System.out.println(k.options[idx][1]);
			System.out.println(k.options[idx][2]);
			System.out.println(k.options[idx][3]);
			System.out.println("   ");
			System.out.println("PRESS 1,2,3 OR 4 : ");


			choice=Integer.parseInt(sc.nextLine());

			if(choice==k.answers[idx])
			{
				System.out.println("CONGRATS!!CORRECT ANSWER...YOU HAVE WON RS." +k.money[i]);
				System.out.println("   ");
				System.out.println("   ");
			}
			else
			{
				System.out.println("WRONG ANSWER!GAME OVER!");
				System.out.println("   ");
				System.out.println("   ");
				break;
			}
		}
	}
}

ERROR:
C:\Users\AMAN\Desktop\NIIT\KB.java:64: cannot find symbol
symbol : variable Arraylist
location: class KB
Arraylist=questions;
^
1 error

Tool completed with exit code 1

The list should be an instance variable. Secondly this part

ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0;i!=questions.length;i++) {
    Arraylist=questions[i];
}

you only want to do once, not with every call to getRandomQuestion

Thirdly, use i < questions.length.

Fourthly, when adding the items to the arraylist use the Add method (see the API docs).

Fifthly, You only want to store "i" in the ArrayList, not "questions[i]".

Sixthly this Random r = new Random(); should also only be done once (not with every call) and r should also be an instance variable.

Seventhly, store the result of "list.get(i)" (no need for the cast), and then return that at the end of the method.

P.S., judging by all the problems, I can only assume that you got the original code here from somewhere else as well, as many of the problems listed above are real beginner things that you should be far past, judging by the rest of the code. And you see how you are sturggling with this? That's because you haven't been doing your own homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.