I would like these objects to print the random names in the array only once across all the objects declared in main. My problem is that it would show the names twice or more.
public class Fighters {
private static String[] names = { "Terminator", "Slicer","Ninja", "cow", "Robot", "littlegirl" };
private static int id = 0;
public Fighters(){
id++;
name = names[(int) (Math.random() * names.length)];
}
public String toString(){
return String.format(id+" Name:%-5s, name);
}
}
In main()
I have these objects
Fighters a1 = new Fighters();
System.out.println(a1.toString());
Fighters a2 = new Fighters();
System.out.println(a2.toString());
Fighters a3 = new Fighters();
System.out.println(a3.toString());
If you want to keep your name assignment in the Fighter class, use a static variable to hold the list of used names. You need to make sure you have not created more Fighters than the names array. Otherwise this will loop indefinitely. I will leave it up to you to figure that part. This should at least get you on the right track.
import java.util.ArrayList;
public class Fighter {
private String name;
private static String[] names = { "Terminator", "Slicer","Ninja",
"cow", "Robot", "littlegirl" };
private static int id = 0;
private static ArrayList<String> usedNames = new ArrayList<String>();
public void Fighters(){
id++;
String checkedName = "";
do{
checkedName = names[(int) (Math.random() * names.length)];
if(!usedNames.contains(checkedName)) break;
}
while(true);
name = checkedName;
usedNames.add(name);
}
public String toString(){
return String.format(id+" Name:%-5s, name");
}
}
Keep the "remaining names" in a List or Set, and when you need to pick one do so randomly & remove it from "remaining".
public class NamePicker {
protected List<String> names = new ArrayList( Arrays.asList(
"Terminator", "Slicer","Ninja", "cow", "Robot", "littlegirl" ));
protected List<String> remaining = names;
public String pickName() {
if (remaining.isEmpty()) {
// could refill 'remaining' here.. but we'll just throw, for now.
throw new IllegalStateException("names all used");
}
int index = (int) (Math.random() * remaining.size());
String result = remaining.remove( index);
return result;
}
}
Putting this behavior in a class also wraps it up nicely, and enables you to better separate "name assignment" for creation of Fighters from eg. loading them from a file or database.
Generally, you shouldn't design just one constructor that assumes that you're "creating" the entity -- you should allow for "load the entity" as well. When you get there, the best way is to have two static methods one for create()
the other for load()
calling onto internal constructors.
its because you ar picking a random element (int) (Math.random() * names.length)..and because of probability, you are picking the same one again by chance
1 option for you problem maybe to randomize the names array and pick in order
2.another option would be to use a list instead of array and remove once used