9 Replies Latest reply: Nov 17, 2009 7:02 AM by 843789 RSS

    How to return a message in a boolean method?

    843789
      I am looking to return a message other than true or false using a boolean method. Is that possible to do without displaying the true, or false?
      private boolean search(Node node, int id){
                if (node == null)
                     return false;
                else if (id == node.id){
                     return true;
                }
                else if (id < node.id){
                     return search(node.left, id);
                }
                else
                     return search(node.right, id);
           }
      In this snippet I would like to return a message rather than true or false.

      My idea was to put the println statement just before the return, but that leaves me with a true or false being displayed.

      I also thought i could use an if statement in my main:
      public class studentTest {
      
           public static void main(String[] args) {
                StudentDatabase data = new StudentDatabase();
                
                System.out.println(data.insert(7066, "Pianczk", 25, 4.0));
                System.out.println(data.insert(7067, "Pianczk", 25, 4.0));
                data.insert(756, "Pia", 78, 3.5);
                boolean result;
                result = data.search(7066);
                if (result){
                     System.out.print("")
                }
           }
      
      }
      the problem with this is I can't reach the info I would like to print out.(or at least I don't know how)
        • 1. Re: How to return a message in a boolean method?
          843789
          I figured it out.

          I thought I tried this once already, but must not have done it correctly.
          private boolean search(Node node, int id){
                    if (node == null){
                         return false;
                    }
                    else if (id == node.id){
                         System.out.print("Student info: " + node.id + " " + node.lastName + " " + node.age + " " + node.gpa);
                         return true;
                    }
                    else if (id < node.id){
                         return search(node.left, id);
                    }
                    else
                         return search(node.right, id);
               }
          public static void main(String[] args) {
                    StudentDatabase data = new StudentDatabase();
                    
                    System.out.println(data.insert(7066, "Pianczk", 25, 4.0));
                    System.out.println(data.insert(7067, "Pianczk", 25, 4.0));
                    data.insert(756, "Pianc", 78, 3.5);
                    int id = 7065;
                    boolean result;
                    result = data.search(id);
                    if (result)
                         System.out.println("");
                    else
                         System.out.print("Your ID " + id + " was not found");
               }
          }
          this works, although I am not sure if this is the optimal way of doing it.
          • 2. Re: How to return a message in a boolean method?
            843789
            edit: nevermind

            Edited by: Encephalopathic on Nov 15, 2009 2:48 PM
            • 3. Re: How to return a message in a boolean method?
              843789
              Just curious, in the future should I go back and edit my original question and void the post?
              This is the second time ive figured something out almost immediately after posting the question.
              • 4. Re: How to return a message in a boolean method?
                EJP
                Best thing to do is post the answer so others will benefit.
                • 5. Re: How to return a message in a boolean method?
                  843789
                  Ive run into a little issue with my search code. The code above is used to recursively search a binary tree.

                  My trouble comes when I want to search the tree and then modify the node I find. What is happening is the info of the node is being displayed. Is there a better way of capturing a node's info than the one I have shown above?
                  • 6. Re: How to return a message in a boolean method?
                    800268
                    The solution you found yourself is more of a hack, which is why you got a problem now.
                    You search method should return the found Node (or Nodes if you allow duplicates). Then you can do with those whatever you want (print out, modify, etc).
                    Node node = data.findNodeByID(7066);
                    boolean found = node != null;
                    • 7. Re: How to return a message in a boolean method?
                      843789
                      If that is the case why return a boolean?
                      You can return a String and can do String comparison in the calling functions.
                      Something like this:

                      private String search(Node node, int id){
                                if (node == null)
                                     return "notPresent";
                                else if (id == node.id){
                                     return "Present";
                                }
                                else if (id < node.id){
                                     return "SomeString";
                                }
                                else
                                     return "SomeString1";
                           }


                      and you can do String comparision i the calling method ..

                      Baba
                      • 8. Re: How to return a message in a boolean method?
                        EJP
                        And how is that better than returning the Node found?

                        A: it isn't.
                        • 9. Re: How to return a message in a boolean method?
                          843789
                          WalterLaan wrote:
                          The solution you found yourself is more of a hack, which is why you got a problem now.
                          You search method should return the found Node (or Nodes if you allow duplicates). Then you can do with those whatever you want (print out, modify, etc).
                          Node node = data.findNodeByID(7066);
                          boolean found = node != null;
                          Thanks for that little tip.