0

I am trying to create a code that would reject an answer that only contains whitespace/spaces. So far I already made a code to reject empty inputs but once the user inputs spaces, the program accepts it.

How do I prevent this? Thank you!

    do {
        while(true) {
            System.out.print("Dog's name: ");
            String dogName = null;
            dogName = scan.nextLine().toLowerCase();
            String n = dogName.replaceAll("\\s", "");

            if(dogName.isEmpty()) {
                System.out.println("Error: Name can't be empty.");
                continue;   
            }
Ricola
  • 2,621
  • 12
  • 22
M Ann
  • 107
  • 1
  • 9

5 Answers5

4

The method trim() would be quite handy in this case. From the documentation :

Returns a copy of the string, with leading and trailing whitespace omitted.

You can use it this way:

while (true) {
    System.out.print("Dog's name: ");
    String dogName = scan.nextLine().trim().toLowerCase();

    if (dogName.isEmpty()) {
        System.out.println("Error: Name can't be empty.");
        continue;
    }
}
Ricola
  • 2,621
  • 12
  • 22
  • It will remove the leading and trainling white spaces only – JineshEP Jan 14 '19 at 11:29
  • 4
    Well if the String only contains whitespaces, then all of the whitespaces are both leading and trailing so the result is an empty String. – Ricola Jan 14 '19 at 11:32
  • 1
    This will alter (trim) valid inputs too. In the case of dog names it's *probably* not going to matter, unless you have a dog called i.e. " Fido" with a leading or trailing space. If you use this code, you should be aware of that. – kscherrer Jan 14 '19 at 12:44
1

Thank you for the help everyone! I just added .trim() to the code :)

So now it's: String dogName = scan.nextLine().toLowerCase().trim();

M Ann
  • 107
  • 1
  • 9
0

You may use String.matches to check for empty strings :

           if (dogName.matches("^\\s*$") || dogName.isEmpty()) {
                System.out.println("Error: Name can't be empty.");
                continue;
            }
JineshEP
  • 738
  • 4
  • 7
0

dogName.trim() will remove leading and trailing white space, if the entire String is made of white space then this will replace all and leave an empty string. e.g.

" dog " -> "dog"

" dog name " -> "dog name"

If checking for empty/blanks is going to be common then take a look at the StringUtils.isNotBlank for "inspiration" and write your own Static util methods based of those.

Pete
  • 205
  • 2
  • 14
0

A simple solution, update your code with

dogName.replaceAll("[^a-zA-Z0-9]", "");

so your code becomes, ...also remember to upadte your if statement.

do {
        while(true) {
            System.out.print("Dog's name: ");
            String dogName = scan.nextLine().toLowerCase();
            String n = dogName.replaceAll("[^a-zA-Z0-9]", "");

            if(n.isEmpty()) {
                System.out.println("Error: Name can't be empty.");
                continue;   
            }
   }
Lai
  • 472
  • 6
  • 23