Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdavron imamov
9,373 PointsWhile loop
hello i am on Craig Davids Java Regular expressions tutorial and i was wondering how to loop the zipCode part of the code so that if the zipCode is not valid it has to input the zipCode again and if the zipCode is valid it outputs the zipCode is valid can someone please show a valid code for this
1 Answer
Yanuar Prakoso
15,196 PointsHi Davron
Here is my while loop code:
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Reggie {
public static void main(String[] args) {
Console console = System.console();
// Your amazing code below
String zipCode;
zipCode = console.readLine("Enter your zipcode: ");
while (!zipCode.matches("^\\d{5}(-\\d{4})?$")){
//Wrong String zipCode example = "90210-000";
System.out.printf("%s is NOT a valid zip code%n", zipCode);
zipCode = console.readLine("Re-Enter your zipcode: ");
}
System.out.printf("%s is a valid zip code%n", zipCode);
}
}
I hope this can help a little
davron imamov
9,373 Pointsdavron imamov
9,373 Pointsoh great it was easier than i was expecting thank you very much for your support