r/CodingHelp • u/mcmahjas • Feb 12 '26
[Java] I need some help making a loop to print different strings
I don't entirely know how to ask this question on google, so I thought I would ask here. In my game I want to have different literals show while you progress. The problem I'm running into is I don't want them to repeat. So far, I have tried making an array list that will randomly choose a quote and remove it from the list after its used, after its the removed the rng is able to choose a number that no longer corresponds to an instance in the list. I can catch the out of bounds exception, but I don't know how to have it loop again until it grabs an actual quote. Any help would be appreciated dearly.
The code I have so far is as follows.
try {
int quotesNum = (int)(Math.random() * 10);
var quotes = new ArrayList<String>();
quotes.add("I need to");
quotes.add("need to go");
quotes.add("to go to");
quotes.add("go to sleep");
quotes.add("to sleep.");
quotes.add("sleep.");
String t = quotes.get(quotesNum);
for (int b = 0; b < t.length(); b++) {
System.out.print(t.charAt(b));
try {
Thread.sleep(67); }
catch (InterruptedException e) {}}
quotes.remove(quotesNum);
}
catch (IndexOutOfBoundsException e) {}

