OnJava8-Examples/interfaces/RandomStrings.java

51 lines
1.3 KiB
Java
Raw Normal View History

// interfaces/RandomStrings.java
2020-10-07 13:35:40 -06:00
// (c)2020 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.*;
public class RandomStrings implements Readable {
2015-06-15 17:47:35 -07:00
private static Random rand = new Random(47);
private static final char[] CAPITALS =
2015-06-15 17:47:35 -07:00
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] LOWERS =
2015-06-15 17:47:35 -07:00
"abcdefghijklmnopqrstuvwxyz".toCharArray();
private static final char[] VOWELS =
2015-06-15 17:47:35 -07:00
"aeiou".toCharArray();
private int count;
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
cb.append(CAPITALS[rand.nextInt(CAPITALS.length)]);
2015-06-15 17:47:35 -07:00
for(int i = 0; i < 4; i++) {
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) {
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
*/