954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Return Boolean to main

Hi,
I have a couple of classes and I am trying to do the following:

I have a main class that contains a boolean value called "display" that displays various messages throughout the class depending on true/false values.

Main class example:

public static void main (String[] args)
   {
     boolean display;

      switch (input1)
            {
             case 'A':   
               System.out.print("please enter value");
               inputInfo = stdin.readLine().trim();
               A.B(inputInfo); // call method B in class A
               
               if (display == true)
                System.out.print("Not Found");
               else
                System.out.print("Found");
               break;
}

Class A example:

public class A{

public boolean B(String input)
{
    //not sure how to return a boolean so it is recognized by main class in switch case A

}

Now the main class calls another class method, we can call the class A and the method B.

Now from method B i am trying to return a value for the boolean that is declared in main class. However i have tried doing something like:

return false;


But that does not work. Does anyone know how i get the main class to recognize the false/true return type from method B and assign it to its own boolean "display" ?

Thanks

spec80
Newbie Poster
14 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
boolean display = A.B(inputInfo);


The return statement you used is legal. You can return false, true, or a boolean variable when using a boolean method. When you call A.B() that line of code becomes (basically anyways) the data type you are returning. That is why if you have a method that returns a String you can say

String testString = Class.returnMethod(someVariable)

As long as returnMethod() returns a String, the above code will work. The same concept applies to your situation.

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

thanks that fixed it.

spec80
Newbie Poster
14 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You