0

I am creating a hotel program which has an array filled with objects (Room), and each Room is filled with a customer name. I used an exchange sort to sort the array alphabetically, however I keep getting an error that Room cannot be converted into a String. I am fairly new to Java and I am struggling to find a fix to this. This is my sort below. How could I overcome this issue?

private static void orderedView(Room hotelRef[]) {
    for (int i = 0; i < hotelRef.length; i++) {
        for (int j = i + 1; j < 12; j++) {

            if (hotelRef[i].compareTo(hotelRef[j]) > 0) {
                String temp;
                temp = hotelRef[i];
                hotelRef[i] = hotelRef[j];
                hotelRef[j]= temp;
            }
        }
    }
    System.out.print("Names in Sorted Order:");
    for (int i = 0; i < hotelRef.length - 1; i++) {
        System.out.println(hotelRef[i] + " ");

    }
    System.out.print(hotelRef[12 - 1]);

}
2
  • 1
    Can you show us more code please? Room would be useful. Commented Mar 7, 2018 at 21:00
  • temp is a Room not a string this line of code is bad. String temp;. I would code. Room mRoomTemp; So I know the class by looking at the variable. Commented Mar 7, 2018 at 21:02

3 Answers 3

1

There are some problems here:

  1. if (hotelRef[i].compareTo(hotelRef[j]) this will work if Room implements Comparable (or you have a custom Room.compareTo) that actually compares by customer name. If not then maybe you need:

    if(hotelRef[i].getCustomerName().compareTo(hotelRef[j].getCustomerName()) > 0)

  2. temp = hotelRef[i];: temp is a String variable and you are trying to assing a Room to it. Change String temp; to Room temp;

If you are on Java-8 or above then you can sort it simpler using Arrays.sort and Comparator.comparing:

Arrays.sort(hotelRef, Comparator.comparing(Room::getCustomerName));
// or whatever the customer name's getter method is named
Sign up to request clarification or add additional context in comments.

Comments

0

When coding Object Oriented Java, you need to create Getters and Setters for retrieving or modifying variables within an object. For example, you would make a function in Room similar to the following:

public String getCustomerName() { return customerName; }

When you call this function on your object, it will return the customer name for you. I hope this helps, and good luck in your future excursions into Object Oriented Java.

Comments

0

Use sort with the Comparator

for example:

Arrays.sort(arr, 0, arr.lenght, (Room lsh, Room rhs) -> {
   return lsh.getName().compareTo(rhs.getName());
});

1 Comment

Note that you can get the same comparator with less code: Comparator.comparing(Room::getName), that's all.

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.