I previously posted a [Reservoir-Sampling code](http://codereview.stackexchange.com/questions/30764/review-of-reservoir-sampling) which was basically a test version of this one. This is an assignment , and in the code below , i use `RunTimeException` to control when scanner finishes reading data. Reasons for this being 1. I cannot use any other library than java.lang 2. I cannot use Scanner either , We have been provided a StdIn class which has a static scanner.next() method only for reading strings. **Note** : All the code is inside the main() because that is how the api demands it. Can someone review my code , and possibly tell if there's some other way i can do this better ? **Edited code :** public class Subset { public static void main(String[] args) { int sampleSize = Integer.parseInt(args[0]); String[] sample = new String[sampleSize]; int i = 0; // initialize the sample from reservoir while(i < sample.length){ sample[i++] = StdIn.readString(); // StdIn : a class in the provided JAR. } try{ for(; ; i++){ int randomIndex = (int)(Math.random()*(i + 1)); if(randomIndex < sampleSize){ sample[randomIndex] = StdIn.readString(); } } }catch(RuntimeException e){ // do nothing } for(String s : sample){ System.out.print(s + " "); } } } **StdIn.readString() :** public static String readString() { return scanner.next(); } **Command line input** : `echo A B C D E F G H I J | java Subset 3` **Output** `A F C`