0

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);
    }
}
Martín Zaragoza
  • 1,523
  • 6
  • 17
  • 4
    Since `split` returns an array, I'm not sure how you expect the `equals("Troll")` to work? – MadProgrammer Sep 24 '18 at 23:39
  • I don't know. I'm just trying to figure out how to create an object based on what the file says. If you can think of a way to do that please let me know. Can I create a new array and use that to compare what the file says? – Lucas Smallwood Sep 24 '18 at 23:42
  • It mostly depends on the actual structure of the file and of your classes. If this were my problem, I would break the problem down into small steps, and then try to solve each issue, one at a time, not the whole thing in one gulp as you're doing. First read the file, and print out each line. Test that this works as expected. Next split each line using `String#split(...)` and print out the array obtained from each line using a for loop. Test to make sure that the output is what you expect. Next, using the array contents, figure out how to use each array item in the line as necessary... – Hovercraft Full Of Eels Sep 25 '18 at 00:11
  • I think you should stop for a second and look towards a more common structure - such as JSON or XML, which already provide a large number of APIs, functionality and support and since they are commonly used in the industry, it would time worth spent – MadProgrammer Sep 25 '18 at 00:12
  • To MadProgrammer I can't use those as I'm doing this for a class that uses eclipse. To Hovercraft my class just needs to add a new object based on the name. The details of the class are already programmed. I just need to add a new object like if the file says "goblin" then I need to add a new goblin object. How do I code that? That's what I'm trying to figure out. If you could provide an example it'd be much appreciated. – Lucas Smallwood Sep 25 '18 at 00:27
  • Also Hovercraft the duplicate you provided was a question I asked a few days ago but it doesn't answer the question I have now. – Lucas Smallwood Sep 25 '18 at 00:34
  • `"I can't use those as I'm doing this for a class that uses eclipse"` ?? this makes no sense. Eclipse is just an IDE, and Java will handle XML and JSON just fine no matter what IDE you may be using. It's like saying, "I can't do this because I'm left handed" -- the two have nothing to do with each other. And yes, the prior question is quite similar, but most important, you need to do the grunt work of working this out. You can do it if you again break the problem down and keep at it, one step at a time. – Hovercraft Full Of Eels Sep 25 '18 at 00:38
  • To Hovercraft Thank you. I didn't know eclipse could handle XML and JSON. I just meant I needed to use eclipse for this program. I thought they were seperate. Okay I'll figure it out. Thank you. – Lucas Smallwood Sep 25 '18 at 00:54

0 Answers0