I created a quote generator and when i click on a person's name, it prints out a random quote of theirs. Now the person has 20 quotes all stored in its own array. How do I get it to print out random quotes tat have not been used, once tehy all have been used, it can repeat.

Recommended Answers

All 2 Replies

A very simplistic way to do it is to have 2 arrays; one to hold the quotes, the second of the same size to indicate whether a quote has been used or not. So, you might initially fill the second array, which indicates whether a quote has been used, with zeros. Let's call this array "indicator". When a quote from the first array (call it quotes) is used, you change the same position in the indicator array to 1. If your random number lands on an array element set to 1 in indicators, you ignore the quote and get another random number. When all the indicator elements are 1, you start over by setting them all to zero again. So, think of how you might use a loop to accomplish this, and some other piece of programming to examine the indicator array for all ones, at which time this other piece resets all elements to zero.

There are many, many ways to accomplish what you want, this is just one way that popped into mind.

That's a perfectly reasonable way to do it, but this being Java please use a boolean array, not a numeric array with 0/1 values.

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.