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;
}
}