I'm trying to add an object based on input from a file. If the first field in the file has a certain name like "goblin" or "troll" then the program should create an object that's respective to that class. In my program I'm trying to use a comparison to split that matches the input from the file but it ends up running infinitely. The problem I'm having is that I can't figure out how to sort the object creation. Like how do I create a new goblin if the file has goblin as it's name? See my code below to see what I'm having problems with. How can I add a new object based on the input from a file? That's what I'm trying to figure out. The else statement is used to create a new enemy object. It takes input from the file to create a new enemy object. It works but the other if statements don't
public void loadOpponents(String filename) {
// TODO
// Replace the code below with code that reads values from
// the given file and creates the appropriate opponent.
// You will need to check whether the first field is Troll,
// Jinn, Wall, etc. to determine the type of opponent to create.
// Once the opponent is created, add it to the opponents array list.
if (input.next().split("Wall").equals("Wall")) {
Wall wall = new Wall();
opponents.add(wall);
} else if (input.next().split("Troll").equals("Troll")) {
Troll troll = new Troll(numVials);
opponents.add(troll);
} else if (input.next().split("Goblin").equals("Goblin")) {
Goblin goblin = new Goblin(numVials);
opponents.add(goblin);
} else if (input.next().split("Jinn").equals("Jinn")) {
Jinn Jinn = new Jinn(numVials);
opponents.add(Jinn);
} else {
String line[] = input.next().split(",");
String name = line[0];
if (line[1] != null && line[1].trim().matches("^[0-9]*$")) {
int strength = Integer.parseInt(line[1]);
}
if (line[2] != null && line[2].trim().matches("^[0-9]*$")) {
int speed = Integer.parseInt(line[2]);
}
if (line[3] != null && line[3].trim().matches("^[0-9]*$")) {
int numVials = Integer.parseInt(line[3]);
}
Enemy newEnemy = new Enemy(name, strength, speed, numVials);
opponents.add(newEnemy);
}
}