the code does not validate spaces and results into moving onto the next line of code to which it should keep in a continious loop until valid data is entered. I have a problem with matching for whitespaces using \s as it doesnt work. many thanks

                    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("^[^a-z,^A-Z,\\s]$")){
                            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("^[^a-z,^A-Z,\\s]$"));

Recommended Answers

All 4 Replies

can any one help please

salesPersonName.matches("^[^a-z,^A-Z,\\s]$")

failes for everything (eg "John"), I dont know much about regex though.

salesPersonName.matches("^[^a-z,^A-Z,\\s]$")

failes for everything (eg "John"), I dont know much about regex though.

thanks for your help, i found a page on here that had bits and pieces, i was only validating one character instead of multicharacters my regular expression required a +
([]+) looks like that <<. i have to figure out how to validate return key i.e. null in regex :(

public class Test {
    public static void main(String[] args) {
    	
    	Scanner in = new Scanner(System.in);
    	
    	System.out.println("type your name");
    	String name = "";
    	
    	boolean accepted = false;
    	
    	while (!accepted)
    	{
    		name = in.nextLine();
    		
    		Pattern p = Pattern.compile("[a-zA-Z]+");
    		Matcher m = p.matcher(name);

    		accepted = m.matches();
    		
    		if (!accepted)
    		{
    			System.out.println("Please try again, no spaces blabla");
    		}
    	}
    	
    	System.out.println("hi " + name);
    }
}

finally got some of the damn regex to work! (this stuff is really confusing and the internet is suprisingly lacking in decent tutorials or very simple examples...) That code works with a-z and A-Z but not numbers, spaces or special characters. What do you mean about validating the return key?

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.