扫描用户输入到数组中的数组超出范围异常

时间:2014-05-02 09:08:32

标签: java arrays exception indexoutofboundsexception

嘿,我有一个项目的代码我试图完成,并且我在某个部分继续获得数组超出范围的异常,并想知道如何解决它。我的项目是使用3个类Room,Reservation和一个客户端类创建一个预订系统。在询问了房间的客人数量后,我的bookRoom课程出现问题。非常感谢任何帮助。例外情况发生在预订类的第57行。有问题的行以粗体显示。

`

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


    }
    }

2 个答案:

答案 0 :(得分:0)

您正尝试使用变量guests索引guests数组,该变量是数组的 size ,您应该使用迭代计数器变量{{1而是:

i应为guestName[guests] = scand.next()

答案 1 :(得分:0)

你的代码中有很多错误。

首先 - 在预订类

中使用以下代码而不是您的版本
  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();
            }

第二 - 在Room类中使用以下方法而不是现有方法。

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

让我知道你取得的成就