0

friends! I just have one quirked up question, I seem to have an issue with my very last question in my assignment.

I'm not sure how to explain it but for some reason, while getValue() works wonderfully in my for loop, it doesn't connect with my if statement, declaring that getValue() cannot be resolved for type House. I have tried casting it over, which activates the getValue(), but then won't allow me to state ">" as an operator.

Error when casting: The operator > is undefined for the argument type(s) package.House

What my method is looking for is the price of the house sold at in the end (soldPrice in my class) and scanning through each key of the hashmap to find which key has the greatest value.

Here's the method! :)

    public House maxPrice(HashMap<Integer, House> obj) {
         Integer max = (Integer)obj.keySet().toArray()[0];
         
         
          for(Map.Entry<Integer, House> entry : obj.entrySet()){
              House currentHouse = entry.getValue();
              if (currentHouse.getValue() > obj.get(max).getValue()) {
                  max = entry.getKey();
              } return obj.get(max);

          } return null;

    }

For some reason, when my professor ran the same code, it worked on his compiler successfully (he had visually the same class), while mine (restarted plenty of times) doesn't. How should I overcome this?

I cannot cast the object to an int, nor an (Integer) wrapper class either.

Thanks so much for any assistance, it is all greatly appreciated!! At a complete roadblock at the moment! Let me know if you need any clarification

Shipra Sarkar
  • 1,385
  • 3
  • 10
noelle
  • 21
  • 5
  • 4
    Without seeing class `House` and its `getValue()` method, nobody can possibly help you. – Jim Garrison Dec 09 '21 at 05:43
  • I understand the House class part, but isn't .getValue a HashMap method? :0 It's not a specific class method, I just might need some understanding on how exactly the getValue works > – noelle Dec 09 '21 at 06:24
  • 3
    You have a variable `House currentHouse` and call the method `currentHouse.getValue()`. For this to compile the class `House` must have a method `getValue()`. Because you try to compare the result of `currentHouse.getValue()` to something else with `>` the return value of `House.getValue()` must be a numeric type (int, double, ...) – Thomas Kläger Dec 09 '21 at 06:31
  • So sorry for the late response!! Thank you very much for your input, both of you! I was able to put it together! I used one of my own methods to get the data I needed instead of just .getValue! my method, .getPrice (of the house) could gather the soldprice for both objects to compare. Thank you once again! :) – noelle Dec 14 '21 at 14:22

0 Answers0