944,057 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 673
  • Java RSS
Oct 17th, 2009
0

Global 'if' statement?

Expand Post »
Is there a way to make an 'if' statement that applies to the whole code?

I'm doing a user-input text-based program. And I basically want the user to be able to type 'quit' at any time, and that'll end the program. But I don't really want to have
Java Syntax (Toggle Plain Text)
  1. if (_line .equals("quit"))
  2. quit();
for every single time there's a bit of text-input.

Is there a way to do this? Any time the user types in 'quit', quit(); runs?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
morgoe is offline Offline
2 posts
since Oct 2009
Oct 17th, 2009
1
Re: Global 'if' statement?
I don't think it's possible. Whenever you type something it is stored in the variable, therefor you need an if statement, but I would suggest to wait for other people responses as well in case someone has a better answer.

Actually there is something that can simulate that.
Write a thread that keeps running in parallel with your main. Whenever you read the input set a local variable defined in the Thread. The Thread in its run method will keep checking the value of that variable in an endless loop. If "quit" is found you will break from the loop and call the quit in the run method.
Although you will need to be careful not to leave any unfinished business in the main, so you might want to pass more variables to the Thread from the main, so it will monitor the state of the main
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 17th, 2009
0
Re: Global 'if' statement?
i don't believe global variables exist in java, only local variables
Reputation Points: 5
Solved Threads: 0
Junior Poster in Training
javaman2 is offline Offline
55 posts
since Jan 2009
Oct 17th, 2009
2
Re: Global 'if' statement?
Write a method that reads the user input (I am assuming your getting input at various stages) and checks whether it is "quit" or not and otherwise returns the input as a String. Then, every place where you are currently reading input call this method instead.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 18th, 2009
0
Re: Global 'if' statement?
@masijade
Could do that but it really wouldn't save much/any lines of code from just running (if _line .equals("quit"))

Oh well, I might just have to do this for every input, thanks anyway.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
morgoe is offline Offline
2 posts
since Oct 2009
Oct 18th, 2009
0
Re: Global 'if' statement?
I don't know how it wouldn't.

Currently you read the input every time so that is 1 line of code for every input. Add the if statement as well as it's action and that is three lines of code for every input.

The method complete is 5 lines (with the closing brace), so, as of the third input read, instead of 9 lines of code it is 8 lines of code and th code is reduced by two lines for every additional read. Anf those calculations are assuming that you only have a single line inside of the if statement. If there are more than it pays off even quicker.

Plus you are not repeating the same code over and over and over again, which is just begging for problems. If what you do in that if block changes, you then have to change every if block (and hopefully you don't forget one), whereas with the method you change one. And what about if you then, later, also need to check for, say "start over" as well as quit? No the space saved has doubled.

But, hey, go ahead and do it your way. It's your choice.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 18th, 2009
0
Re: Global 'if' statement?
Click to Expand / Collapse  Quote originally posted by masijade ...
Write a method that reads the user input (I am assuming your getting input at various stages) and checks whether it is "quit" or not and otherwise returns the input as a String. Then, every place where you are currently reading input call this method instead.
Clever suggestion. Although I was under the impression that he wanted something done automatically - like in the background.
But even with my solution, you would still need to call a set method, unless the variable was static, but anyway, I think it would be a lot of fuss about nothing.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007

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: IntelliJ IDEA Goes Open Source
Next Thread in Java Forum Timeline: tic-tac-toe problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC