Cleaning Data in Java
Dennis Lee
Software Engineer
Title | Publish Date | Rating | Review Count | Price |
---|---|---|---|---|
Python Crash Course | 1/10/23 | 4.8 | 165 | $30.61 |
9/13/19 | 4.8 | 2,521 | $38.00 | |
Clean Code | 8/1/08 | [null] | [null] | [null] |
// book is missing reviewCount, rating, and price BookSales book = new BookSales("Clean Code", LocalDate.of(2008, 8, 1), null, null, null)
// rating is null, so the code will throw a NullPointerException int rating = book.rating();
Exception in thread "main" java.lang.NullPointerException
// Import Objects
import java.util.Objects;
// Use .isNull()
if (Objects.isNull(book.rating())) {
System.out.println("Missing rating");
}
Missing rating
import java.util.Optional;
// Use Optional.ofNullable for numbers
double rating = Optional.ofNullable(book.rating()).orElse(0.0);
System.out.println("Book rating: " + rating);
Book rating: 0.0
// Use Objects.toString for strings
String displayTitle = Objects.toString(book.title(), "[No Title]");
System.out.println(displayTitle);
Book title: [No Title]
import org.apache.commons.lang3.StringUtils; // Utility for string operations
// book is missing a title BookSales book = new BookSales(" ", LocalDate.of(2019, 9, 13), 2521, 4.5, 38.00)
if StringUtils.isBlank(book.title()) { System.out.println("Invalid - empty title"); }
Invalid - empty title
// priceText is blank
String priceText = " ";
// Check for blank values before converting to a number
if StringUtils.isBlank(priceText) {
System.out.println("Invalid price: blank detected");
}
Invalid price: blank detected
java.util.Objects
and java.util.Optional
NullPointerException
by checking nulls before useObjects.isNull()
to check for nullsOptional.ofNullable()
to provide defaultsorg.apache.commons.lang3.StringUtils
StringUtils.isBlank()
to detect "", null, or whitespaceCleaning Data in Java