0

In my code I have a class named Item with two fields (String name and int weight). Fields are set as private so I created a getter to access their values. It works when I use it in regular way in main method like:

Item item=New Item("ItemName", 25);
System.out.println(item.getItemName);

Later in another class I created a method that reads a lines in the file including items (They are formated like "ItemName="1000 so I used some char replacment and parseInt). The whole method definition looks like this:

public static ArrayList loadItems(String fileName) throws FileNotFoundException {
        ArrayList<Item> itemsList=new ArrayList();
        File file=new File(fileName);
        Scanner fileScannerNazwy=new Scanner(file);
        Scanner fileScannerWagi=new Scanner(file);
        while (fileScannerNazwy.hasNextLine()||fileScannerWagi.hasNextLine()){
            String lName=(fileScannerNazwy.nextLine().replaceAll("[\\d]","")).replace("=","");
            int lWeight=Integer.parseInt(fileScannerWagi.nextLine().replaceAll("[\\D]",""));
            Item item=new Item(lName, lWeight);
            itemsList.add(item);
        }
        return itemList;
}

Now the problem is that I don't know how to get acces to the fields of individual Item objects stored in ArrayList returned by the method above. Using in main method something like:

(Classname.loadItems("filename.txt").get(0)).getItemName();

Does not seem to work.

EDIT per request. Item class looks like this:

public class Item {
     private String itemName;
     private int itemWeight;

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

    public String getItemName() {
        return itemName;
    }
    public int getItemWeight() {
        return itemWeight;
    }
}
ancanar
  • 1
  • 2
  • Please provide the relevant part of the `Item` class to create a [mre]. – Unmitigated Dec 14 '21 at 19:31
  • Don't you need to specify the return type of your loadItems() method as ArrayList rather than just ArrayList? The compiler might be objecting because it cannot determine the type of object within the list. – M. Gianota Dec 14 '21 at 19:34
  • Please mention the error/issue you are getting – user699848 Dec 14 '21 at 19:35
  • By the way, `replaceAll("[\\D]","")` looks like a naive form of parsing. Each non-digit is removed, so for example, `wh4t in th3 w0rld is thi5` yields `4305`. I would reject it instead. – MC Emperor Dec 14 '21 at 19:36
  • 1
    What makes you think that it "Does not seem to work"? Are you getting some exception, or compilation error, or some results different than what you expected? BTW you shouldn't be using two scanners to read from one file at the same time. Use one Scanner and handle line it is returning to create *one* Item. – Pshemo Dec 14 '21 at 19:37
  • M. Gianota - thnank You. That was the problem. Return type lacked generic . I wasn't getting any errors or exception. IDE just did not let me use getter method cause as I suppose the unspecific return type was causing the array list type to be casted to Object where it should be of Class Item. – ancanar Dec 14 '21 at 19:48
  • Pshemo - What problem can be caused by using two scanners. Actually I was strugging to get name and weight separated and found here on SO advice to use double scanners. – ancanar Dec 14 '21 at 19:59
  • @ancanar It makes your code unclear. What is the point of using two scanners in the first place? What problem ware you facing while using single scanner? Based on your example all you need is something like `while(scanner.hasNextLine()){ String[] parts = scanner.nextLine().split("="); String name = parts[0].substring(1,parts[0].length-1); int weight = Integer.parseInt(parts[1]); itemsList.add(new Item(name, weight));`. – Pshemo Dec 14 '21 at 20:23

0 Answers0