user2403571
user2403571

Reputation:

trying to compare object cannot use IF statement JAVA

I am writing a method to find an object in an ArrayList. If I can find the object I will print it out to the screen, otherwise I will print an error message saying "Object not found". The problem I am running into is since my method is the object, "Dodecahedron", and not a boolean, I cannot use an if statement to compare if the object exists in the array. How else could I approach this?

This is the code in my main method.

    System.out.print("\tLabel: ");
    label = userInput.nextLine();

    if(myDList.findDodecahedron(label)) {

        System.out.print(myDList.findDodecahedron(label));
    }
    else {
        System.out.print("\t\"" + label + "\" not found");
    }
        System.out.print("\n\nEnter Code [R, P, S, A, D, F, E, or Q]: ");
    break;

and this is my method.

public Dodecahedron findDodecahedron(String label1In) {
      String label = "";
      String color = "";
      double edge = 0;
      Dodecahedron result = new Dodecahedron(label, color, edge);
      int index = -1;
      for (Dodecahedron d : dList) {
         if (d.getLabel().equalsIgnoreCase(label1In)) { 
            index = dList.indexOf(d);
            break;
         }    
      }
      if (index >= 0) {
         dList.get(index);
         result = dList.get(index);
      }
      return result;
   }

And this is the error I get when I try to compile.

DodecahedronListAppMenu.java:99: error: incompatible types: Dodecahedron cannot be converted to boolean
               if(myDList.findDodecahedron(label)) {

Upvotes: 1

Views: 90

Answers (4)

Andreas
Andreas

Reputation: 159135

First, don't call the find method twice. Assign the result to a local variable for reuse.

Multiple choices:

  1. Since you create a dummy object with empty label, which is returned if not found, you can check for that.

    Dodecahedron result = myDList.findDodecahedron(label);
    if (result.getLabel().isEmpty()) {
        System.out.print("\t\"" + label + "\" not found");
    } else {
        System.out.print(result);
    }
    

    Alternative is the null object design pattern described in answer by Mureinik.

  2. Change method to return null for not found.

    public Dodecahedron findDodecahedron(String label1In) {
        for (Dodecahedron d : dList) {
            if (d.getLabel().equalsIgnoreCase(label1In)) {
                return d;
            }
        }
        return null;
    }
    

    Use:

    Dodecahedron result = myDList.findDodecahedron(label);
    if (result != null) {
        System.out.print(result);
    } else {
        System.out.print("\t\"" + label + "\" not found");
    }
    
  3. Change method to return Optional (Java 8+).

    public Optional<Dodecahedron> findDodecahedron(String label1In) {
        for (Dodecahedron d : dList) {
            if (d.getLabel().equalsIgnoreCase(label1In)) {
                return Optional.of(d);
            }
        }
        return Optional.empty();
    }
    

    Alternative for returning Optional is the use of streams.

    public Optional<Dodecahedron> findDodecahedron(String label1In) {
        return dList.stream()
                    .filter(d -> d.getLabel().equalsIgnoreCase(label1In))
                    .findFirst();
    }
    

    Use:

    Optional<Dodecahedron> result = myDList.findDodecahedron(label);
    if (result.isPresent()) {
        System.out.print(result.get());
    } else {
        System.out.print("\t\"" + label + "\" not found");
    }
    

Option 3 using streams is the recommended solution.

Upvotes: 0

Mureinik
Mureinik

Reputation: 311808

You are essentially implementing the null object design pattern here. You can make this explicit in the findDodecahedron method (side note - I replaced the implementation with Java 8-style streams for elegance, but it's not really required):

public static final Dodecahedron NULL_DODECAHEDRON = new Dodecahedron("", "", 0);
public Dodecahedron findDodecahedron(String label1In) {
      return dList.stream()
                  .filter(d -> d.getLabel().equalsIgnoreCase(label1In))
                  .findFirst()
                  .orElse(NULL_DODECAHEDRON);
}

And then use it in the if condition:

if (!myDList.findDodecahedron(label).equals(NULL_DODECAHEDRON)) {
    System.out.print(myDList.findDodecahedron(label));
} else {
    System.out.print("\t\"" + label + "\" not found");
}

Upvotes: 0

davidxxx
davidxxx

Reputation: 131456

If your method doesn't return a boolean but a specific object you should assign it into a variable to exploit it. You don't instantiate a specific object and return it to test it like a boolean value. And because of that, you have to repeat the method invocation to retrieve a second time the result as you need to print it in the std output. It is helpless and a duplicate code/processing.

This should look like :

Dodecahedron obj = myDList.findDodecahedron(label)
if(obj != null) {
    System.out.print(obj);
}
else {
    System.out.print("\t\"" + label + "\" not found");
}

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361849

Check if the return value is null.

if (myDList.findDodecahedron(label) != null)

You'll need to update findDodecahedron() to return null if it doesn't find anything, rather than a new object. Changing the initial value of result will do that:

Dodecahedron result = null;

Alternatively, you could get rid of index and result if you just return the shape immediately when you find it. No need to save its index off and then look the index up after the loop ends.

public Dodecahedron findDodecahedron(String label1In) {
   for (Dodecahedron d : dList) {
      if (d.getLabel().equalsIgnoreCase(label1In)) { 
         return d;
      }    
   }
   return null;
}

You might also streamline it a bit further with Java 8 streams:

public Dodecahedron findDodecahedron(String label1In) {
   return dList.stream()
      .filter(d -> d.getLabel().equalsIgnoreCase(label1In))
      .findAny()
      .orElse(null);
}

Upvotes: 1

Related Questions