Data type verification
1. Data type verification
Let's talk about verifying data types!2. Why check data types?
External data from files, user input, or other sources may contain formatting that can cause program crashes if not validated. For example, attempting to parse "$30.61" with `Double.parseDouble()` fails with a `NumberFormatException` due to the dollar sign. We'll learn to safely convert text data for numbers and dates.3. Validating numeric text
From the `NumberUtils` library, `NumberUtils.isParsable()` checks strings before numeric conversion. It returns `true` for valid numbers like "165" but `false` for formatted values like "$30.61" because of the dollar sign. This helps prevent runtime errors when processing external data.4. Parsing numeric text
Let's examine how to convert text to numbers safely. The `validateNumeric()` helper method uses `NumberUtils.isParsable()` to check if a string can be parsed as a number. For integers like review counts, we use `validateNumeric()` followed by `Integer.parseInt()`. For decimals like prices, we remove formatting characters (like a dollar sign) and call `validateNumeric()` before using `Double.parseDouble()`. preventing `NumberFormatException` errors when processing text data.5. Validating date formats
Next let's see how to parse dates. The `parseDate` method converts date strings into `LocalDate` objects using a specific format pattern. We create a `DateTimeFormatter` with the pattern month/day/year which expects dates like "1/10/23". The `LocalDate.parse()` method uses this formatter to convert the string into a proper date object. In this example, "1/10/23" is successfully parsed into "2023-01-10", following the default date format for `LocalDate`.6. Combining numeric and date validation
Now let's format currencies. The `parsePrice()` method handles currency formatting by removing the dollar sign before parsing to a double. We then use this method along with other validation methods in `parseSalesData()` to convert text data into a strongly-typed `BookSales` record. Each field gets appropriate parsing: strings for titles, `parseDate()` for dates, `validateNumeric()` with `.parseInt()`/`.parseDouble()` for reviews and ratings, and `parsePrice()` for currency values.7. Reading CSV files safely
Let's read a CSV file of book sales data. First, we create an empty `sales` list to store our `BookSales` records. The `BufferedReader` helps us read the file line by line, and the `try` block ensures our file is closed properly after reading. We skip the header row with `.readLine()`, then process each remaining line: split it at commas using `.split()` to get an array of fields, convert these fields into a `BookSales` record using `parseSalesData()`, and add the record to our list. Finally, we print each sale using `.forEach()`. The double colon `::` is a shorthand way to pass a method as a parameter.8. Summary: data type verification
When parsing text data, we validate the format before conversion to prevent errors. We use `NumberUtils.isParsable()` for numbers and `DateTimeFormatter` for dates, combining these checks to ensure data integrity throughout our application.9. Let's practice!
Now you can practice validating data types!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.