0

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.

CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
Veranicus
  • 27
  • 5
  • In your file, you have a different item on each line? – Sweeper Sep 17 '17 at 11:00
  • This is exact file i am reading it from. https://ufile.io/479dw Oh my, there are no spaces in between. Problem is I have downloaded the files from here https://d17h27t6h515a5.cloudfront.net/topher/2017/August/59a4e7fc_phase-1/phase-1.txt , each is on the new line but when i downloaded it, in editor it changed one after each other without spaces. I noticed it now. – Veranicus Sep 17 '17 at 11:15

2 Answers2

0

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++) {
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Yes, but the upper boundary in the for loop should still be changed to name.size() – dsp_user Sep 17 '17 at 11:24
  • Oh my, so I practically get an list of arrays, each consisted of two elements, so when i get from each one of them 0, it will be the name and from another one 1, it will be an integer, going to try it out now. – Veranicus Sep 17 '17 at 11:25
  • @dsp_user I noticed that just now! Edited. – Sweeper Sep 17 '17 at 11:26
  • And is this some short description ? For example Integer.parseInt(name.get(i)[1]) , is it a shortcut from some longer code ? Never seen this before and it's working so just asking. – Veranicus Sep 17 '17 at 11:41
  • @Veranicus `name.get(i)` returns an array, right? Arrays can be accessed with square brackets, right? So there is nothing short with this code, but I guess you could put the `name.get(i)` into a variable called `itemArray` and access it like `itemArray[1]`. If you think my answer answers your question, please consider accepting it by clicking on that checkmark!' – Sweeper Sep 17 '17 at 11:43
0

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])));
}
CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
dsp_user
  • 2,061
  • 2
  • 16
  • 23