jeudi 27 septembre 2018

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)) {

Aucun commentaire:

Enregistrer un commentaire