2016-12-04 09:30:08 -08:00
|
|
|
// interfaces/RandomStrings.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Implementing an interface to conform to a method
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.nio.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
2016-12-04 09:30:08 -08:00
|
|
|
public class RandomStrings implements Readable {
|
2015-06-15 17:47:35 -07:00
|
|
|
private static Random rand = new Random(47);
|
2016-11-21 12:37:57 -08:00
|
|
|
private static final char[] CAPITALS =
|
2015-06-15 17:47:35 -07:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
|
2016-11-21 12:37:57 -08:00
|
|
|
private static final char[] LOWERS =
|
2015-06-15 17:47:35 -07:00
|
|
|
"abcdefghijklmnopqrstuvwxyz".toCharArray();
|
2016-11-21 12:37:57 -08:00
|
|
|
private static final char[] VOWELS =
|
2015-06-15 17:47:35 -07:00
|
|
|
"aeiou".toCharArray();
|
|
|
|
private int count;
|
2016-12-04 09:30:08 -08:00
|
|
|
public RandomStrings(int count) { this.count = count; }
|
2015-06-15 17:47:35 -07:00
|
|
|
@Override
|
|
|
|
public int read(CharBuffer cb) {
|
|
|
|
if(count-- == 0)
|
|
|
|
return -1; // Indicates end of input
|
2016-11-21 12:37:57 -08:00
|
|
|
cb.append(CAPITALS[rand.nextInt(CAPITALS.length)]);
|
2015-06-15 17:47:35 -07:00
|
|
|
for(int i = 0; i < 4; i++) {
|
2016-11-21 12:37:57 -08:00
|
|
|
cb.append(VOWELS[rand.nextInt(VOWELS.length)]);
|
|
|
|
cb.append(LOWERS[rand.nextInt(LOWERS.length)]);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
cb.append(" ");
|
|
|
|
return 10; // Number of characters appended
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
2016-12-04 09:30:08 -08:00
|
|
|
Scanner s = new Scanner(new RandomStrings(10));
|
2015-06-15 17:47:35 -07:00
|
|
|
while(s.hasNext())
|
|
|
|
System.out.println(s.next());
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Yazeruyac
|
|
|
|
Fowenucor
|
|
|
|
Goeazimom
|
|
|
|
Raeuuacio
|
|
|
|
Nuoadesiw
|
|
|
|
Hageaikux
|
|
|
|
Ruqicibui
|
|
|
|
Numasetih
|
|
|
|
Kuuuuozog
|
|
|
|
Waqizeyoy
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|