Question:
Java: how to loop a program to restart if a certain certain value is returned?
?
2013-04-02 14:07:53 UTC
I am writing a date validation program. I need help with this one part. I wrote a method to do the validation part, (there are more parts to the program but this is the only one that matters.) How do I tell the main method to restart if Invalid is returned from this method?

For example, I have a program that asks this "Please insert a date in MM/DD/YYYY format or press Q to quit). a user inputs 05/94/1992. This will return invalid. I need the program to produce an error message (System.out.print("Error");) then loop to the beginning of the program and once again. How do I do this? Also, how to I set up Q to quit the program?
Four answers:
The Shadowman
2013-04-02 14:36:41 UTC
What do you mean by "loop to the beginning of the program and once again."? If you mean to go all the way back to the first line in the main method, then basically you'll need to use for, while, or do while loop to get it work. If you mean to go back to the question and ask them to enter a valid answer before moving on, then you need to use for, while, or do while loop right where you ask the question. Here's an example:



public static void main (String[] args) {

do { //a while loop still works here

variable blah blah = method (blah blah);

output error message

}

while (data is invalid && answer != "Q");

}



Or loop back to the question only:



method (blah blah) {

ask question

store answer

while (answer != "Q" && answer is invalid) {

output error message

re-ask question

re-store answer

}

if (answer == "Q")

return;

}
Barolb
2013-04-02 21:35:42 UTC
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
Jay
2013-04-02 21:51:10 UTC
ceate a boolean variable and set it as false at the beginning. once u get to the part yu want to start over, say this:



while(!variable){

//insert code here

if(true)(

variable=true;

}

else{

variable=false;

}







if it happens, you mak it come out of the loop by settin thevariable to the opposite value

if it does not happen, set the variable needed to redo the loop or, if boolean or utouched, leave it as it is and java will automatically redo te loop
?
2013-04-02 21:26:34 UTC
Here you go:

http://pastebin.com/qZ2pHNJ4



The loop runs until the user enters either q/Q or a valid date.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...