0

Hoping for some help - I've been asked to write a hotel room system using methods for uni. All has been going well until I try to order the array alphabetically.

I have managed to get it to order within the method but it updated the main array (hotel). I want it to keep it within the order method, if that makes sense?

I've included a cut down version below without all the functions.

Currently it will reorder the array hotel so if you view the rooms the array will print like 'e,e,e,etc, George, Peter, Robert' instead of keeping its original form 'e, Robert, Peter, e,e,etc, George'

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String roomName;
    int roomNum = 0;
    String[] hotelRef = new String[12];
    String[] hotel = new String[12];
    initialise(hotel); //initialise
    while (roomNum < 13) {
        System.out.println("Please select from the menu:");
        System.out.println("V : View rooms");
        System.out.println("O : Order Guests alphabetically");
        String selection = input.next();
        switch (selection) {
            //There are more switch cases on the original version
            case "O":
                order(hotel);
                break;
            default:
                System.out.println("");
        }
    }
}

private static void order(String[] hotelRef) {
    int j;
    boolean flag = true; //will determine when the sort is finished
    String temp;
    String[] order = new String[12];
    order = hotelRef;

    while (flag) {
        flag = false;
        for (j = 0; j < order.length - 1; j++) {
            if (order[j].compareToIgnoreCase(order[j + 1]) > 0) { 
                //ascending sort
                temp = order[j];
                order[j] = order[j + 1]; // swapping
                order[j + 1] = temp;
                flag = true;
            }
        }
    }
    for (int y = 0; y < order.length; y++) {
        if (!order[y].equals("e")) {
            System.out.println("Room " + y + " is occupied by " + order[y]);
        }
    }
    System.out.println("Ordering completed");
}
L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
George
  • 19
  • 9
  • 3
    [Clone the array](https://stackoverflow.com/questions/14149733/clone-method-for-java-arrays). – Compass Feb 22 '18 at 15:57
  • 1
    `order = hotelRef` here you unintentionally (instead of cloning the array) just made order also point to the values in hotelRef and all changes would then also reflect in hotelRef – Exception_al Feb 22 '18 at 15:59
  • Don't use the original array if you don't want to make changes to it. Create a copy of the array what gives you the opportunity to also filter its content and avoid displaying the empty rooms... – DIEGO CARRASCAL Feb 22 '18 at 16:00

3 Answers3

1

You should clone the hotelRef instead of assigning the reference like this order = hotelRef;

You could do the following while creating the order array :

String[] order = new String[hotelRef.length]; // to make sure that order has the right size.

and instead of order = hotelRef;

for (int i=0;i<order.length;i++)
   order[i]=hotelRef[i]; // thereby cloning

or use System.arraycopy() or any other method to accomplish cloning the array.

Exception_al
  • 1,049
  • 1
  • 11
  • 21
0

You can make copy of hotel array in your order method:

String[] hotelCopy = new String[hotelRef.length];
System.arraycopy(hotelRef, 0, hotelCopy, 0, hotelRef.length);

And then just use hotelCopy inside your order method.

gmpf
  • 236
  • 2
  • 11
0

The problem lies with the following line

order = hotelRef;

Change it to

order = hotelRef.clone();

Though you are creating a new object, you have assigned the reference to outer object only. So whatever changes you make in the inner object it will be reflected to the outer object.

vbbharath
  • 127
  • 4