bpham93
bpham93

Reputation: 1

convert a integer to array of class type java

I take in a file which has a name (table) and the number of seats:

table1 6

table2 2

table3 4

I have an array of class Reservation which will take in the the name and the seat. I am having trouble converting the number of seats into the array. How do i go about doing so?

public class Reservable {

protected String id;
private Reservation[] arrayRes = new Reservation[10];



public Reservable (Scanner fileIn) {
    while(fileIn.hasNext()) {
        id = fileIn.next();
        for(int i = 0; i < arrayRes.length; i++) {
            int seat = fileIn.nextInt();
            arrayRes[i] = seat;

        }
    }


}

here is my Reservation class:

public class Reservation {
    private String name;
    private int timeSlot;
    private Reservable myReservable;

    public Reservation(String name, int timeSlot) {
        this.name = name;
        this.timeSlot = timeSlot;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTimeSlot() {
        return timeSlot;
    }

    public void setTimeSlot(int timeSlot) {
        this.timeSlot = timeSlot;
    }

    public Reservable getMyReservable() {
        return myReservable;
    }

    public void setMyReservable(Reservable myReservable) {
        this.myReservable = myReservable;
    }

    public boolean isActive() {
        return false;
    }

Upvotes: 0

Views: 38

Answers (2)

davidxxx
davidxxx

Reputation: 131496

You can read line by line since your file has a reservation by line. I propose you to have a Reservation constructor with two parameters (name and nbSeat).
A remark : you array of reservation has a fixed size : 10. If you file has more than 10 elements, a ArrayIndexOutOfBoundsException will be risen.
If the number of reservation may be superior to 10 or is variable, you should use a List rather than a array.

protected String id;
private Reservation[] arrayRes = new Reservation[10];


public Reservable (Scanner fileIn) {
    int i=0;
    while(fileIn.hasNextLine()) {
        String line = fileIn.nextLine();
        String[] token = line.split("\\s");
        String name = token[0];
        int nbSeat= Integer.valueOf(token[1)];
         // add in your array
         Reservation reservation = new Reservation(name,nbSeat);
        arrayRes[i] = reservation ;

    }
      i++;
}

And Reservation :

public class Reservation{

  public Reservation(String name, int nbSeat){
    this.name=name;
    this.nbSeat=nbSeat;
  }
 }

Upvotes: 1

tomSurge
tomSurge

Reputation: 256

You need to show us what your Reservation class looks like, but if it uses the conventional getters and setters this is the correct way:

public Reservable (Scanner fileIn) {
    while(fileIn.hasNext()) {

        for(int i = 0; i < arrayRes.length; i++) {
            int seat = fileIn.nextInt();
            arrayRes[i].setSeat(seat);
            id = fileIn.next();
            arrayRes[i].setId(id);

            }
        }
}

Upvotes: 0

Related Questions