| | |
Global 'if' statement?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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?
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)
if (_line .equals("quit")) quit();
Is there a way to do this? Any time the user types in 'quit', quit(); runs?
1
#2 Oct 17th, 2009
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
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
Check out my New Bike at my Public Profile at the "About Me" tab
2
#4 Oct 17th, 2009
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
0
#6 Oct 18th, 2009
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.
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
0
#7 Oct 18th, 2009
•
•
•
•
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.
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.
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- confusion with global statement. (Python)
- Climate change. Global march on Bali (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: IntelliJ IDEA Goes Open Source
- Next Thread: tic-tac-toe problem
Views: 372 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor database draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop machine map method methods mobile netbeans newbie nls number object oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test textfield threads time transfer tree unlimited webservices windows






