Return Boolean to main

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 14
Reputation: spec80 is an unknown quantity at this point 
Solved Threads: 0
spec80 spec80 is offline Offline
Newbie Poster

Return Boolean to main

 
0
  #1
Mar 16th, 2009
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:

  1. public static void main (String[] args)
  2. {
  3. boolean display;
  4.  
  5. switch (input1)
  6. {
  7. case 'A':
  8. System.out.print("please enter value");
  9. inputInfo = stdin.readLine().trim();
  10. A.B(inputInfo); // call method B in class A
  11.  
  12. if (display == true)
  13. System.out.print("Not Found");
  14. else
  15. System.out.print("Found");
  16. break;
  17. }
Class A example:

public class A{

  1. public boolean B(String input)
  2. {
  3. //not sure how to return a boolean so it is recognized by main class in switch case A
  4.  
  5. }
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:

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Return Boolean to main

 
1
  #2
Mar 16th, 2009
  1. 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
  1. String testString = Class.returnMethod(someVariable)
As long as returnMethod() returns a String, the above code will work. The same concept applies to your situation.
Last edited by jasimp; Mar 16th, 2009 at 11:17 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 14
Reputation: spec80 is an unknown quantity at this point 
Solved Threads: 0
spec80 spec80 is offline Offline
Newbie Poster

Re: Return Boolean to main

 
0
  #3
Mar 16th, 2009
thanks that fixed it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC