Вопрос

Hey I have code for a project im trying to finish up and I keep getting array out of bounds exceptions at a certain part and would like to know how to fix it. My project is to create a reservation system using 3 classes Room,Reservation and a client class. I'm having trouble with my bookRoom class after asking for the number of guests for the room.Any help would be greatly appreciated. The exception occurs at line 57 in the reservation class. The line in question is in bold.

`

public class Reservation {
    public void roomInitialization(Room[] room,int numberOfRooms,int numberOfSmokingRooms)
    {
        boolean smoking=true;
        String[] guestName={null,null,null,null};

        for (int i=0;i<numberOfRooms;i++)
        {
        if (i>=numberOfSmokingRooms)
            smoking=false;
        room[i]=new Room(smoking,false,guestName,null,i+1);
        }
    }
    public  void displayRoomsInfo(Room[] room,int numberOfRooms)
    {
         for (int i=0;i<numberOfRooms;i++)
         {
        System.out.println("Room " + room[i].getroomNumber() + "\t" + 
                     (room[i].getsmoking()?"Smoking room":"Non-Smoking Room"));
        if (room[i].getoccupied())
        {
            System.out.print("Phone: "+room[i].getguestPhone()+"\t");
            for(int j=0;j<4;j++)
                System.out.print(room[i].getguestName()[j]+"\t");
            System.out.println();
        }
         }
    }



        public void bookRoom (Room[] room, int numberOfRooms)
         {
            displayRoomsInfo(room, numberOfRooms);  
            Scanner scan = new Scanner(System.in);
            Scanner scans = new Scanner(System.in);
            Scanner scand = new Scanner(System.in);
            System.out.print("Enter a Room Number: ");
            int roomNumber = scan.nextInt()-1;
            if (roomNumber >=0 && roomNumber < 30){
                room[roomNumber].setoccupied(true);
                System.out.print("Enter a Phone Number: ");
                String guestPhone = scans.nextLine();
                room[roomNumber].setguestPhone(guestPhone);

                System.out.print("Enter number of guests:(Max of 4 per room) ");
                int guests = scand.nextInt()-1;
                String[] guestName = new String[guests];
                for (int i=0;i<guestName.length;i++)
                    System.out.print("Enter guest names: ");

                    **guestName[guests] = scand.next();**
                room[roomNumber].setguestName(guestName);


            }
            else
                System.out.println("Enter a valid room number");


         }




    public void checkOut(Room[] room)
    {
         Scanner scan=new Scanner(System.in);
         int roomNumber;
         String[] nullguestName={null,null,null,null};
         do
         {
        System.out.print("Enter the room number: ");
        roomNumber=scan.nextInt()-1;
        if (roomNumber>=0 && roomNumber<30)
            break;
        else
            System.out.println("Enter a valid room number");
         }while(true);
         if (room[roomNumber].getoccupied())
         {
        room[roomNumber].setoccupied(false);
        room[roomNumber].setguestPhone(null);
        room[roomNumber].setguestName(nullguestName);
         }
         else
        System.out.println("room "+(roomNumber+1)+" is already empty");
    }


}
`
Code for the other classes



        package hotel;

    public class Room {
        private boolean smoking;
        private boolean occupied;
        private String[] guestName=new String[4];
        private String guestPhone;
        private int roomNumber;

        public Room (boolean smoking,boolean occupied,String[] guestName, String guestPhone,int roomNumber)
        {
            this.smoking=smoking;
            this.occupied=occupied;
            for (int i=0;i<4;i++)
                this.guestName[i]=guestName[i];
            this.guestPhone=guestPhone;
            this.roomNumber=roomNumber;
        }


        public void setoccupied(boolean occupied) {
            this.occupied = occupied;

        }

        public void setsmoking(boolean smoking) {
            this.smoking = smoking;

        }
        public void setroomNumber(int roomNumber) {
            this.roomNumber = roomNumber;

        }

        public void setguestPhone(String guestPhone) {
            this.guestPhone = guestPhone;

        }
        public void setguestName(String[] guestName)
        {
            for (int i=0;i<4;i++)
                this.guestName[i]=guestName[i];
        }

        public boolean getsmoking() {
            return this.smoking;
        }

        public boolean getoccupied(){
        return this.occupied;
    }
    public String getguestPhone(){
        return this.guestPhone;
    }
    public int getroomNumber() {
        return this.roomNumber;
    }



    public String[] getguestName()
    {
        String[] tempguestName=new String[4];
        for (int i=0;i<4;i++)
            tempguestName[i]=this.guestName[i];
        return tempguestName;
    }




    }


    package hotel;

    import java.util.Scanner;

    public class ReservationDemo {
        public static void main(String[]args)
        {
            final int numberOfRooms=30, numberOfSmokingRooms=5;
            Room[] room=new Room[numberOfRooms];
            Reservation reservation=new Reservation();
            reservation.roomInitialization(room,numberOfRooms,numberOfSmokingRooms);
            int userSelection;
            Scanner scan=new Scanner(System.in);
            do
            {
            System.out.println("Press: 1-Book room\t2-checkout\t3-display all rooms\t4-exit. ");
            userSelection=scan.nextInt();
            switch(userSelection)
            {
            case 1:

                reservation.bookRoom(room,numberOfRooms);
                break;
            case 2:
                reservation.checkOut(room);
                break;
            case 3: 
                reservation.displayRoomsInfo(room,numberOfRooms);
                break;
            case 4:
                System.exit(0);
            default:
                break;
            }
            }while (true);


    }
    }
Это было полезно?

Решение

There are lots of mistake in your code.

First-- Use the following code instead of your version in Reservation class

  System.out.print("Enter number of guests:(Max of 4 per room) ");
                int guests = scand.nextInt();
                String[] guestName = new String[guests];
                for (int i=0;i<guestName.length;i++) {
                    System.out.print("Enter guest names: ");
                    guestName[i] = scand.next();
            }

Second -- Use the following method in Room class instead of the existing one.

public void setguestName(String[] guestName)
    {
        for (int i=0;i<guestName.length;i++)
            this.guestName[i]=guestName[i];
    }

Let me know what you achieve

Другие советы

You are trying to index into the guests array using the variable guests which is the size of the array, you should be using the iteration counter variable i instead:

guestName[guests] = scand.next() should be guestName[i] = scand.next()

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top