I want to convert a String
to an object. I looked for related topics but could not find an answer. My code is:
amountShip = 5;
String name = ReadConsole.nextString(); //-->variabe of the object "Player"
gamers[i] = Player.String2Object(name, 0);
Player String2Object(String name, int amountShip) {
Object o = name;
Player temp = (Player)o;
temp = new Player(name, amountShip);
return temp;
}
class Player {
Player (String name, int amountShip) {
name = name;
ships = amountShip;
}
Player pl = new Player(String name, int amountShip);
}
it says
java.lang.String cannot be cast to Gameobjects.Player
My intention is to create 2 Player-objects with different instance names dynamically. If for example somebody types in "3", i want to create
Player p1 = new Player(x,y);
Player p2 = new Player(x,y);
Player p3 = new Player(x,y);
Thanks a lot in advance