课程: Java Algorithms

Validate Strings in Java: All-or-nothing properties - Java教程

课程: Java Algorithms

Validate Strings in Java: All-or-nothing properties

- [Instructor] One type of algorithm you can create is a validation algorithm. You can validate whether a given input string has certain properties or attributes. For example, you can create an algorithm that checks if a string has all uppercase or all lowercase letters. You can also create an algorithm that validates whether or not the string has all alphabetical characters, or if it has at least one number. For a validation algorithm, the output is a Boolean, either true or false. The string either has the given properties or it does not. Let's try creating some validation algorithms in Java. The first validation algorithm we'll create is a function that checks whether a string has all uppercase letters. We'll call it isUppercase, and it will take in a string. There are several helpful tools in the standard library that can help us out with the implementation. First, we'll need to iterate through the string in order to access each character. If each character is uppercase, then the whole string must be uppercase. We can do this using the Java Streams API. We'll create a character stream that streams through the characters and checks that every character is uppercase. To create a character stream, we'll use the chars method. To check the validity of each character, we'll use the allMatch function. This will check that every character meets the condition within the parentheses. For the condition, we'll check if the character is uppercase. To do that, we can use the character's isUppercase function. We cannot use this function directly on the string because it only works with characters. However, we can iterate through the string and use this function as a helper. If all the characters are uppercase, then this function will return true. Otherwise, it will return false. So we'll just return it. Many beginner programmers might wrap this statement as an if/else statement. However, this if statement is not needed, because the allMatch function will return a Boolean, true or false, and it's the correct Boolean that we want our function to return. This means we can keep it as a one-liner. Return s.chars allMatch Character isUppercase. We can also create a similar function, but for lowercase strings. All we have to do is switch the condition. Instead of isUppercase, we'll use isLowercase. Another option is to use noneMatch. This checks that no characters have a given property. In this case, that property is that there are no uppercase letters. In fact, there are lots of different properties we can check that are built into the character class. With isLetter, we can check that the string only contains letters. With isDigit, we can check that the string only contains digits. These are other examples of validations you can perform to determine whether a string has a set of properties or not. Let's try out our validation algorithms. We'll run the main function, and in the output, we should get false true true false. And that's exactly what we see. The all uppercase and all lowercase strings are validated as expected. With these algorithms, we're able to validate an all-or-nothing property, whether or not the entire string was uppercase or lowercase.

内容