Global 'if' statement?

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

Join Date: Oct 2009
Posts: 2
Reputation: morgoe is an unknown quantity at this point 
Solved Threads: 0
morgoe morgoe is offline Offline
Newbie Poster

Global 'if' statement?

 
0
  #1
Oct 17th, 2009
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
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 55
Reputation: javaman2 has a little shameless behaviour in the past 
Solved Threads: 0
javaman2 javaman2 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 17th, 2009
i don't believe global variables exist in java, only local variables
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: morgoe is an unknown quantity at this point 
Solved Threads: 0
morgoe morgoe is offline Offline
Newbie Poster
 
0
  #5
Oct 18th, 2009
@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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
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.
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #7
Oct 18th, 2009
Originally Posted by masijade View Post
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum


Views: 372 | Replies: 6
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC