I’m learning Java, and I’m stuck on the Robot Name, where my code won’t pass 1 of the tests.
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
public class Robot {
private String name;
private Random random;
private static Set<String> names;
public Robot() {
names = new HashSet<>();
this.random = new Random();
generateName();
}
String getName() {
return this.name;
}
void reset() {
generateName();
}
private void generateName() {
String name;
do {
name = generateNameLetters() + generateNameNumbers();
} while (names.contains(name));
this.name = name;
names.add(name);
}
private String generateNameLetters() {
return random
.ints(2, 65, 91)
.mapToObj(codePoint -> (char) codePoint)
.map(String::valueOf)
.collect(Collectors.joining());
}
private String generateNameNumbers() {
return random
.ints(3, 0, 10)
.mapToObj(String::valueOf)
.collect(Collectors.joining());
}
}
I’m failing test robotNamesAreUnique
, where it generates 5000 unique names, and my code just falls short of 5000 and I just don’t know why. Any ideas?