Skip to content

Instantly share code, notes, and snippets.

@mrunderline
Last active May 11, 2021 06:34
Show Gist options
  • Save mrunderline/91204c2e978a4e47b18a2f7cb83f189b to your computer and use it in GitHub Desktop.
Save mrunderline/91204c2e978a4e47b18a2f7cb83f189b to your computer and use it in GitHub Desktop.
a short code for selecting (sth) randomly from a list in java!
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.Collections;
import java.util.Scanner;
public class randomEmail {
public static void main(String[] args) {
try {
Scanner scan = new Scanner(System.in);
System.out.print("Enter path: ");
String path = scan.nextLine();
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
List < String > list = new ArrayList < String > ();
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
String[] listArr = list.toArray(new String[0]);
ArrayList < Integer > numberList = new ArrayList < Integer > ();
for (int i = 0; i < listArr.length; i++) {
numberList.add(i);
}
Collections.shuffle(numberList);
System.out.print("Enter number: ");
int num = scan.nextInt();
if (num > listArr.length) {
System.out.println("Number is more than list length, try again :)");
return;
} else if (num < 1) {
System.out.println("Error number, try again :)");
return;
}
for (int i = 0; i < num; i++) {
String thisRoundStr = listArr[numberList.get(i)];
System.out.print((i + 1) + ". ");
for (int j = 0; j < thisRoundStr.length(); j++) {
System.out.print(thisRoundStr.charAt(j));
TimeUnit.MILLISECONDS.sleep(50);
}
System.out.print("\n");
}
System.out.println("Done!");
} catch (Exception e) {
System.out.println("Error!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment