0

I want to create a custom object with three ints and a String and store that object in an arrayList, but I seem to be having issues with it and haven't been able to find documentation for exactly my issue online. I'm getting errors on the fac.add. here is the code

**made some changes to the code

package facility;
import dal.DataAccess;

public class FacilityInfo implements Facility {

private int buildingNo, roomNo, capacity;; 
private String type; //classroom, conference room, office, etc.

FacilityInfo(){}//default constructor

FacilityInfo(int b, int r, int c, String t){
    this.buildingNo = b; 
    this.roomNo = r; 
    this.capacity = c; 
    this.type = t; 
} 
package dal;
import java.util.*;

import facility.FacilityInfo;

public class DataAccess {
    List<FacilityInfo> fac = new ArrayList<FacilityInfo>();
    fac.add(new FacilityInfo (1,2,10,conference));//changed code here
}
4
  • There's an extra semicolon on the line where you're declaring the 3 int fields. Commented Feb 18, 2017 at 21:21
  • Sidenote : Also you don't need to use this. to refer to the global variable unless the variable name is the same as the constructor parameters. Commented Feb 18, 2017 at 21:22
  • @MasterYushi The default constructor is only done automatically when there are no defined constructors. Commented Feb 18, 2017 at 21:23
  • @4castle Thanks for pointing that out Commented Feb 18, 2017 at 21:27

1 Answer 1

2

That's because of two main reasons.

First, 1,2,10,conference isn't a FacilityInfo object. You can't add the arguments of a FacilityInfo to the List, you have to add an actual object.

Second, you can't have statements outside of a code block, and currently you are calling fac.add(...); directly in the class body.

Try something like:

public class DataAccess {
    List<FacilityInfo> fac = new ArrayList<FacilityInfo>();

    public void initializeFac() {
        fac.add(new FacilityInfo(1,2,10,"conference"));
        // etc.
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

There's also the issue of how fac.add is being called in the class body, and not in a method.
Yes, method calls need to be inside a method
thanks i did this and created a method for it and put it there instead of the class body and this worked
@4castle Oops! Thanks for pointing that out. I've updated the answer to include both errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.