java programmer, whitespaces matching - HotUKDeals
We use cookie files to improve site functionality and personalisation. By continuing to use HUKD, you accept our cookie and privacy policy.
Get the HUKD app free at Google Play

Search Error

An error occurred when searching, please try again!

Login / Sign UpSubmit

java programmer, whitespaces matching

andyhunter Avatar
5y, 10m agoPosted 5 years, 10 months ago
currently having a major problem with whitespaces and it detects a whitespace only if the keyboard if pressed once and if you press it more than once it does not work. i have setup a do while loop to try and contain invalid data but its only working for characters and not white spaces...help would be greatly thanked.

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]$"));
andyhunter Avatar
5y, 10m agoPosted 5 years, 10 months ago
Options

All Comments

(6) Jump to unreadPost a comment
Comments/page:
#1
Is there not a trim function you can use?
#2
That's because your regex is matching a single space, slap a + on it to match more than one
#3
do i add the plus after \\s or at the start before ^ ? cheers
#5
i got it thanks to your good self, just need to find a way to loop nulls from return key, i think \\r is for returns
#6
Why don't you use salesPersonName.contains(" ");

Post a Comment

You don't need an account to leave a comment. Just enter your email address. We'll keep it private.

...OR log in with your social account

...OR comment using your social account

Thanks for your comment! Keep it up!
We just need to have a quick look and it will be live soon.
The community is happy to hear your opinion! Keep contributing!