-1

I am trying to sort an array of objects by field, but I keep getting this error saying that "price / location cannot be resolved or is not a field", even though they both are fields, declared in the class Realestate. I got the same error about "not being a field" when I was dealing with ArrayList as well, so I moved to simple arrays of objects and I'm still getting the same error. What am I doing wrong? I found info about comparing arrays with Comparable and Comparator but I don't undertand how to apply whatever results to the sorting routine. Please HELP! These are my files:

File Realestate:

public class Realestate {
    static String location = "";
    static String description = "";
    static String price = "";

    // constructor
    public Realestate(String loc, String desc, String prc) {
        location = loc;
        description = desc;
        price = prc;
    }
}

File RealestateFinder:

import java.util.*;

public class RealestateFinder {

    // Hold properties' info
    static String[] locations = {"Miami", "Las Vegas", "Paris", "London"};
    static String[] descriptions = {"1-bedroom", "2-bedrooms", "3-bedrooms", "penthouse"};
    static String[] prices = {"1,000,000", "2,000,000", "3,000,000", "4,000,000"};

    // Sort per field
    public static void sortArray(Realestate arr[]) {
        for(int x = 0; x < arr.length; ++x) {            
            //use array Sort
            Arrays.sort(arr);
        }           
    }

    public static void main(String[] args) {        
        int numProperties = 4;

        //Create new Realestate object newProperty
        Realestate[] newProperty = new Realestate[numProperties]; 

        // Initialize array of objects Realestate[] newProperty using arrays from top
        for(int x = 0; x < newProperty.length; ++x) {
            newProperty[x] = new Realestate(locations[x], descriptions[x], prices[x]); // constructor
        }

        //Get user's input
        //...

        while(true) {
            if(holdUserInput == 1) {
                //sort per price
                sortArray(newProperty.price);//price cannot be resolved or is not a field
                break;
            }

            else if(holdUserInput == 2) {
                // sort per location
                sortArray(newProperty.location);//location cannot be resolved or is not a field
                break;
            }

            else {
                System.out.println("Program Ended \n");
                break;
            }// end else statement
        }// end while loop
    }// end main()
}// end class RealestateFinder
2
  • 4
    Well yes, newProperty is an array - the array doesn't have a price... Sounds like you might want to read stackoverflow.com/questions/5932720 Commented Jul 17, 2017 at 16:45
  • 1
    Your question has multiple problems unrelated to your question Commented Jul 17, 2017 at 16:47

1 Answer 1

2

First you may not use static for your 3 fields, static means that it's not relative to a particular instance, which i not what you want to

Then use a List is easier to sort so :

int numProperties = 4;
List<Realestate> list = new ArrayList<>();
for (int x = 0; x < numProperties; ++x) {
    list.add(new Realestate(locations[x], descriptions[x], prices[x]));
}

Then, use existing methods to sort is quicker and easier to use :

while (true) {
    if (holdUserInput == 1) {
        list.sort(Comparator.comparing(Realestate::getPrice));
        break;
    } else if (holdUserInput == 2) {
        list.sort(Comparator.comparing(Realestate::getLocation));
        break;
    } else if (holdUserInput == 2) {
        list.sort(Comparator.comparing(Realestate::getDescription));
        break;
    } else {
        System.out.println("Program Ended \n");
        break;
    }// end else statement
}

And finally, because there is not sense to use while(true) and break, better use switch which is better :

switch (holdUserInput) {
    case 1:
        list.sort(Comparator.comparing(Realestate::getPrice));
        break;
    case 2:
        list.sort(Comparator.comparing(Realestate::getLocation));
        break;
    case 3:
        list.sort(Comparator.comparing(Realestate::getDescription));
        break;
    default:
        System.out.println("Program Ended \n");
        break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, @azro, your answer was really helpful, not just for this code, but also to my understanding of a few concepts :)

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.