文件扫描程序在非空行

时间:2015-12-25 21:39:25

标签: java java.util.scanner java-io

我目前正在对一些代码进行单元测试,以保存和恢复游戏数据。我当前的保存文件只是文本文件。我的单元测试保存文件,然后加载它们并将结果与​​原始文件进行比较。

现在我在几个测试中遇到问题,其中我用来读取保存文件的Java扫描器类将在文件行返回空字符串它的阅读并非空洞。返回""后,扫描仪不会移动到下一行,而是读取它返回时应该读取的行""。我的代码需要一个int,但它会从文件中读取一个字符串,并抛出异常。

示例是一个ItemContainer类(就像玩家的背包)。代码调用一个循环来加载ItemContainer中包含的每个项目,并且它成功加载了第一个项目,然后这个bug在循环的第二次迭代中显示出来。

public class UnitTest {
    @Test
    public void testItemContainerContainsCorrectItemsAfterSaveLoad(){
        try {

            ItemContainer saved = Inventory.getStartingBackpack();
            Weapon wpn1 = new Weapon(1, 1, 1, 1);
            wpn1.description = "this is weapon number one";
            wpn1.name = "weapon one";
            Weapon wpn2 = new Weapon(2, 2, 2, 2);
            wpn2.name = "weapon two";
            wpn2.description = "this is weapon number two";
            WornItem clothing = new WornItem(10, 10, 10, 2, 0);
            clothing.name = "clothing";
            clothing.description = "this is clothing";

            saved.addItem(wpn1);
            saved.addItem(wpn2);
            saved.addItem(clothing);

            PrintWriter writer = new PrintWriter(filePath);
            saved.saveToFile(writer);
            writer.close();

            Scanner scanner = new Scanner(new File(filePath));
            ItemContainer loaded = ItemContainer.loadAlpha0_1(scanner);
            scanner.close();

            Field field = ItemContainer.class.getDeclaredField("items");
            field.setAccessible(true);
            ArrayList<Item> savedItems = ((ArrayList<Item>) field.get(saved));
            ArrayList<Item> loadedItems = ((ArrayList<Item>) field.get(loaded));

            for (Item item : savedItems){
                assertTrue(loadedItems.contains(item));
            }

        } catch (FileNotFoundException e) {
            fail("threw exception");
        } catch (NoSuchFieldException e) {
            fail("threw exception");
        } catch (SecurityException e) {
            fail("threw exception");
        } catch (IllegalArgumentException e) {
            fail("threw exception");
        } catch (IllegalAccessException e) {
            fail("threw exception");
        }   
    }
}
public class ItemContainer {
    public static ItemContainer loadAlpha0_1(Scanner scanner){
        int concealmentHits, concealability, spaceUsed, weightContained, baseSpace, baseWeight;
        String name = scanner.nextLine();
        concealmentHits = scanner.nextInt();
        concealability = scanner.nextInt();
        spaceUsed = scanner.nextInt();
        weightContained = scanner.nextInt();
        baseSpace = scanner.nextInt();
        baseWeight = scanner.nextInt();
        ArrayList<Item> items = loadItemsAlpha0_1(scanner);
        return new ItemContainer(baseWeight, baseSpace, name, items);
    }
    private static ArrayList<Item> loadItemsAlpha0_1(Scanner scanner) {
        ArrayList<Item> items = new ArrayList<Item>();
//      scanner.nextLine();
        // This is my bandaid fix for an unexplained blank line "" that keeps being read
        // at certain points in the code.
        scanner.nextLine();
        String itemType = scanner.nextLine();
        while(!itemType.equals("end item container")) {
            Item item = Item.loadAlpha0_1(scanner, itemType);
            items.add(item);
            itemType = scanner.nextLine(); // this line is when the bug mentioned shows up
        }
        return items;
    }
}
public class Weapon extends Item {
    public static Weapon loadAlpha0_1(Scanner scanner) {
        int size, weight, durability, hardness, damage, weaponType, parry, accuracy, might;
        String name = scanner.nextLine();
        size = scanner.nextInt();
        weight = scanner.nextInt();
        durability = scanner.nextInt();
        hardness = scanner.nextInt();
        damage = scanner.nextInt();
        String description = Item.loadItemDescriptionAlpha0_1(scanner);
        weaponType = scanner.nextInt();
        parry = scanner.nextInt();
        accuracy = scanner.nextInt();
        might = scanner.nextInt();
        Weapon weapon =  new Weapon(size, weight, durability, hardness, damage,
                weaponType, parry, accuracy, might);
        weapon.name = name;
        weapon.description = description;
        return weapon;
    }
}

这是测试期间正在读取的文件

backpack
0
0
50
50
1000
10
weapon
weapon one
15
15
125
5
0
this is weapon number one
end description
1
1
1
1
weapon
weapon two
25
25
150
5
0
this is weapon number two
end description
2
2
2
2
worn item
clothing
10
10
10
2
0
end item container

我知道要分享很多代码,但我已经试图解决这个问题很长一段时间了,而且我还没能做到。

编辑:我发现了一个我在创建错误并尝试调试此错误时创建的错误。我已经纠正过了。

0 个答案:

没有答案