This is where using while loops come in handy.
A while loop has a condition where so long as the condition is met, it will continue to run. Basically, you'll be using boolean for this.
while (input != false)
{
}
or
while (input == true)
{
}
are the same thing. while the input meets the condition, where the condition is originally either true or false, then it will continue to run.
if (userInput.equalsIgnoreCase("q"))
{
input = false; // or input = true; depending on which way you do it.
}
That if statement would be the key for this portion. If the input is true, then the input becomes false if the userInput matches, or if the input is false, then the input is true if the userInput matches.
As for quitting the program, you won't need any special way of quitting the program if you setup your program well. All you need to do is to get out of that loop, right? If it goes back to the main method, it continues on, in the program, from the same line (or the line after, depending on if that method returns a value). If you still want a method of exiting the program, then "System.exit(0);" (ignore the quotes, i've placed them there as that is the exact code) is a common way of exiting a program. The 0 in the parenthesis are there as an argument, meaning that there are no abnormalities with the program upon exiting.
Good luck,
~Barolb
Edit:
Time to bring in a bit of discrete mathematics.
Not true, and false are the same thing, so likewise not false and true are the same thing. If you set the condition to be false, the condition will continue to run so long as it's false. When the condition becomes true, then it's nolonger false, and thus the condition that is required for the loop to run is no longer being met. it's the same thing as saying:
while (input != false)
This condition is true so long as the input is false. The user input and the condition's truth value are both different truth values, thus if the input is true, then the condition becomes false and will no longer execute. just think about the condition's truth value being different from the boolean truth value you set input to be. As long as the condition is being met, the truth value is TRUE, and if it's no longer being met, then the truth value is FALSE.
basically, you would use an if statement. If the date is valid, then return a value that makes the while loop's condition nolonger true. if a invalid date is entered, then return a value then will allow the condition to remain true.
input = validation(date);
That's all you need. if the while loop you're using is as follows, then it would be:
while (input == true)
{
input = validation(date);
}
in the validation method it would be as follows:
public boolean validation (String val)
{
if (/*String is valid*/)
{
return false;
}
else
{
return true;
}
}
Once the value of input, which is true, changes to false, then the while loop's condition will no longer be true, and thus the program can get out of the loop and/or end.
And to re-answer, a while loop can do just that. Here's a quick test you can do to confirm what i'm saying:
Scanner keyboard = new Scanner(System.in);
int number;
boolean input = true;
while (input == true)
{
System.out.println("Enter a number between 1 and 10");
number = keyboard.nextInt();
if (number >= 1 && number <= 10)
{
input = false;
}
else
{
System.out.println("Invalid input");
}
}
So long as the number is between 1-10, the condition will become false, but otherwise it will continue to ask a user to enter a valid input.
One other thing to add. If you want to "restart" the program, even after a valid input is entered, then just recall the method again that has the while loop in it, but make sure to have it as an if else, so that way if the user does not wish to re-enter any values, then they can just finish and exit the program.
~Barolb