简体   繁体   中英

Reading part String and part Integer and storing it to an object in Java

I need a functionalitty in my application, a method, to which you put as an argument link to an basic txt file, where are things stored in a simple format like this: "habitat=100000colony=50000..." I have an item class and an item object with String name and integer weight. In the file there is always first name which may be more than a one word, then "=" and then int as a weight. I have so far written this, but have a problem to make it work, so I i would be gratefull for some kind of a help.

This is the object which it is going to be stored into:

public class Item {
    private String name;
    private int weight;

    public Item(String name, int weight) {
        this.name = name;
        this.weight = weight;
    }
    ...
}

And then this is the method:

    public ArrayList<Item> loadItems(File file) throws Exception {
        ArrayList<String[]> name = new ArrayList<>();

        Scanner scanner = new Scanner(file);
        ArrayList<Item> items = new ArrayList<>();

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            name.add(line.split("="));
        }

        for (int i = 0; i < name.size() + 1; i++) {
            items.add(new Item(Arrays.toString(name.get(i)), Integer.parseInt(Arrays.toString(name.get(i + 1)))));
        }

        return items;
    }

When i run the simulation method with proper file, it says this:

Exception in thread "main" java.lang.NumberFormatException: For input string: "[building tools, 3000]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.example.patrickpolacek.Simulation.loadItems(Simulation.java:26)
at com.example.patrickpolacek.Main.main(Main.java:11)

Could it not be working that when the file gets to the last item, to add i + 1 will try to parse as a int blank space and that gives an error ? Thanks once more.

Here is where the error lies:

items.add(new Item(Arrays.toString(name.get(i)), Integer.parseInt(Arrays.toString(name.get(i + 1)))));

You are using Arrays.toString , which will return a string like "["habitat, 1000"]" . This is not what you wanted, is it?

What you should instead do is to get the string array from the array list and get the first and second element of the array , not the array list.

items.add(
    new Item(
        name.get(i)[0], // first item of the array e.g. "colony"
        Integer.parseInt(name.get(i)[1]) // second item e.g. 10000
    )
);

Also, your for loop is a little off. You should loop until name.size() , not name.size() + 1 :

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

Just change the items populating part to something like

for (int i = 0; i < name.size() ; i++) {
    String[] parsed = name.get(i);
    items.add(new Item(parsed[0],Integer.parseInt(parsed[1])));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM