Smalls
Smalls

Reputation: 1

Fill ArrayList from Text file with multiple element types Java

I want to fill an Arraylist from a .txt file however, the ArrayList contains objects (items) with multiple 'variables'.

The class Item 'variables' are String & boolean.

public Item(String aName, String aDescription, boolean canTake) {

This is what I have in a different class to create/fill the ArrayList

public List<Item> loadItems() throws FileNotFoundException{
    File f = new File("Item.txt");      
      Scanner sn = new Scanner(f);
         while (sn.hasNext()){
            String itm = sn.nextLine();
            Scanner sc = new Scanner(itm);
            sc.useDelimiter("#"); 
            String aName = sc.next();
            String aDescription=sc.next();
            Boolean canTake = sc.nextBoolean();
            addItem(aName, aDescription, canTake);
                }
                return items;
            }

In a third class I create another ArrayList and call the loadItems() method so that I can reference specific item objects (i.e. item objects at element 0 of the list).

    public Actions() throws FileNotFoundException {
        loadItems();
        this.itemSlist = loadItems();

ItemList HomeList = new ItemList();
        HomeList.add(itemSlist.get(0));
        HomeList.add(itemSlist.get(1));

The problem is this doesn't actually fill the ArrayList & my output is: [] [] [] Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 0

I know with ArrayLists you would generally just list.add(); but there a multiple variables for each object - not just a string or boolean value.

I've tried moving the loadItems method to different classes and tried using a basic scanner with the txtfile and can access single word item objects but not the multi variable ones.

Upvotes: 0

Views: 168

Answers (1)

Steffi
Steffi

Reputation: 331

Try the following approach:

public class Main{

    public static void main(String[] args) {
        
        ArrayList<Item> listItem = new List().getList();
        System.out.println("Name: " + listItem.get(0).getaName());
        System.out.println("Description: " + listItem.get(0).getaDescription());
        System.out.println("CanTake: " + listItem.get(0).isCanTake());

    }
}

Class List:

public class List {
    
           public ArrayList<Item> getList(){
                    
                ArrayList<Item> listItems = new ArrayList<>();
                    listItems.add(new Item("Name 1", "Description 1", true));
                    listItems.add(new Item("Name 2", "Description 2", false));
                    listItems.add(new Item("Name 3", "Description 3", true));
                    listItems.add(new Item("Name 4", "Description 4", false));
                    listItems.add(new Item("Name 5", "Description 5", false));
                return listItems;
            }        
    }

Class Item:

public class Item {
    
    private String Name;
    private String Description;
    private boolean CanTake;

    public Item(String Name, String Description, boolean CanTake) {
        this.Name = Name;
        this.Description = Description;
        this.CanTake = CanTake;
    }
    
    public String getaName() {
        return Name;
    }

    public void setaName(String aName) {
        this.Name = aName;
    }

    public String getaDescription() {
        return Description;
    }

    public void setaDescription(String aDescription) {
        this.Description = aDescription;
    }

    public boolean isCanTake() {
        return CanTake;
    }

    public void setCanTake(boolean canTake) {
        this.CanTake = canTake;
    }
    
}

Results:

run:
Name: Name 1
Description: Description 1
CanTake: true
BUILD SUCCESSFUL (total time: 0 seconds)

I haven't checked the import of the txt file

Steffi

Upvotes: 0

Related Questions