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

if (_line .equals("quit"))
	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?

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

i don't believe global variables exist in java, only local variables

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.

commented: Good suggestion :) +6
commented: Clever suggestion +4

@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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.