Start New Discussion within our Software Development Community

Stuck on trying to limit a persons name to say 20 characters in a string or a phone number to 6 characters in a string, i cant get the {} inside or after regex to work with either a minimium or maximium number so that in my loop if a persons name is over 20 chracters then it will continual loop or i.e. phone number is less than 6 or more than 6 chracters it will keep looping, any help would be great the internet does not have better explation for this stuff so any one that understands it would be doing me a huge favour to understand this complex stuff, thanks.. sample code would be

                    do{
                        /*if salesPersonName is null or just spaces then this
                          piece of code runs, displaus output and asks for input
                          which salespersoname = input 2
                        */
                        if (salesPersonName.matches("^[\\s]+$")||salesPersonName.matches("")||salesPersonName.matches("^[0-9]+$")){
                            System.out.println("Sales person's name can not"
                                    + " be empty or have space, must contain "
                                    + "a-z characters");
                            System.out.println("Enter Sales Person Name;");
                            salesPersonName = input2.nextLine();
                        }
                      //keeps looping until input is not null or contains just spaces
                    } while (salesPersonName.matches("^[\\s]+$")||salesPersonName.matches("")||salesPersonName.matches("^[0-9]+$"));

You have to provide a lower aswell as an upper bound or your expression will return true at the first match e.g

//Sanitize salesPersonName first

salesPersonName.matches( "[a-zA-Z]{20,20}");

As a tip, it's redundant to be doing the test twice in your code, use a normal while loop rather than do/while.

This article has been dead for over six months. Start a new discussion instead.