943,739 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 10280
  • Java RSS
Mar 16th, 2009
0

Return Boolean to main

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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{

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spec80 is offline Offline
14 posts
since Mar 2008
Mar 16th, 2009
1

Re: Return Boolean to main

Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 16th, 2009
0

Re: Return Boolean to main

thanks that fixed it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spec80 is offline Offline
14 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Simple JTable Data Issue
Next Thread in Java Forum Timeline: urgent!! need help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC