Click here to Skip to main content
14,662,527 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi there, so basically I'm having some trouble initializing some values from a text file:


4 4 1
Warrior: 2
0 0 1 Air 1 30.5 30 72 2 e
1 1 2 Flame 1 85.5 30 72 2 e
Weapon: 2
0 0 3
0 1 30



I'm fine with the warrior bit, however the weapon section is where I struggle. Essentially I want to read in the values 0 0 3 and 0 1 30 as different attributes for the two weapon objects I have to create.

For example:

weapon1 would have row of 0, col of 0 and strength of 3. Meanwhile weapon 2 would have row of 0, col of 1 and strength of 30.

What I have tried:

So far I have turned the textfile into an array list, which each element being a line.

 for (int i = 0; i < list.size(); i++) {

                if (list.get(i).contains("Stone")) {

                    /*Here we are making an attributes array for each line. This is so
                    for each line we can easily parse the attributes and set them through the methods
                            below.*/

                    StoneWarrior_23722002 s = new StoneWarrior_23722002();
                    final String[] attributes = list.get(i).split(" ");
                    boardArray[Integer.parseInt(attributes[0])][Integer.parseInt(attributes[1])] = s;
                    s.setChar('S');
                    setWarriorAttributes(s, attributes);
                    warriorList.add(s);
                    pieceList.add(s);


                } else if (list.get(i).contains("Air")) {

                    AirWarrior_23722002 a = new AirWarrior_23722002();
                    final String[] attributes = list.get(i).split(" ");
                    boardArray[Integer.parseInt(attributes[0])][Integer.parseInt(attributes[1])] = a;
                    a.setChar('A');
                    setWarriorAttributes(a, attributes);
                    warriorList.add(a);
                    pieceList.add(a);


                } else if (list.get(i).contains("Water") && list.get(i).length() > 8) {

                    WaterWarrior_23722002 w = new WaterWarrior_23722002();
                    final String[] attributes = list.get(i).split(" ");
                    boardArray[Integer.parseInt(attributes[0])][Integer.parseInt(attributes[1])] = w;
                    w.setChar('W');
                    setWarriorAttributes(w, attributes);
                    warriorList.add(w);
                    pieceList.add(w);

                } else if (list.get(i).contains("Flame")) {

                    FlameWarrior_23722002 f = new FlameWarrior_23722002();
                    final String[] attributes = list.get(i).split(" ");
                    boardArray[Integer.parseInt(attributes[0])][Integer.parseInt(attributes[1])] = f;
                    f.setChar('F');
                    setWarriorAttributes(f, attributes);
                    warriorList.add(f);
                    pieceList.add(f);
                }

                /*else if (list.get(i).contains("Magic")){

                    while (!(list.get(i).contains("Water") || !(list.get(i).contains("Weapon")))){

                        MagicCrystal_23722002 mc = new MagicCrystal_23722002();
                        final String[] attributes = list.get(i++).split(" ");

                        boardArray[Integer.parseInt(attributes[0])][Integer.parseInt(attributes[1])] = mc;
                        mc.setChar('c');
                        pieceList.add(mc);

                        magicCrystalCount++;


                    }



                }*/

                else if (list.get(i).contains("Weapon")){

                    final String[] atttributes1 = list.get(i).split(":");
                    int cap = Integer.parseInt(atttributes1[1]);

                    for (int j = i; j < cap; j++){

                        final String[] attributes2 = list.get(i).split(" ");
                        Weapon_23722002 weap = new Weapon_23722002();
                        boardArray[Integer.parseInt(attributes2[0])][Integer.parseInt(attributes2[1])] = weap;
                        weap.setWeaponOffPower(Double.parseDouble(attributes2[2]));
                        weap.setChar('x');
                        pieceList.add(weap);
                        weaponList.add(weap);
                    }

                }



}




So as you can see, I loop through the array list and set the attributes appropriately. How can I get to the weapon section, and then set its attributes?
Posted
Updated 13-Oct-20 22:48pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

for (int j = i; j < cap; j++){

You are starting your weapon attribute counter with whatever is in variable i. However, you should start at zero for a correct count of the attributes.

My mistake, I think it should be:
for (int j = 0; j < cap; j++){

    final String[] attributes2 = list.get(i).split(" ");
    Weapon_23722002 weap = new Weapon_23722002();
    Weapon_23722002 weap = new Weapon_23722002();
    boardArray[Integer.parseInt(attributes2[0])][Integer.parseInt(attributes2[1])] = weap;
    weap.setWeaponOffPower(Double.parseDouble(attributes2[2]));
    weap.setChar('x');
    pieceList.add(weap);
    weaponList.add(weap);
    i++; // increment i to next weapon
}
   
v4
Comments
helloThere555 14-Oct-20 6:47am
   
I appreciate the feedback man, but I changed it and nothing changed :(
Richard MacCutchan 14-Oct-20 7:09am
   
See my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100