Handling Invalid Data in Programming
Invalid Data Handling
Introduction to Invalid Data
- Focus has been primarily on valid data in selection and iteration examples.
- Need to address invalid data, which is unsuitable input that needs to be managed.
- Invalid data can be managed by:
- Implicitly fixing the issue.
- Prompting the user for new, valid data.
Scenario: Apple Sales Calculation
- Expanded version of apple sales calculation example.
- Original example only considered positive numbers or zero for days and apples sold.
- Now includes handling negative numbers for both.
- If the user enters a negative number for the number of days, the program will:
- Print an error message.
- Exit.
- If the user enters a negative number for apples sold each day, the program will:
- Prompt the user to input a positive number.
- Repeat the request until a valid input is given.
Review of Original Code's Handling of Invalid Data
- Original code used a user-specified threshold for the number of days to iterate.
- Control variable initialized to zero and incremented until it's no longer less than the threshold.
- If a negative number is entered for days:
- The loop is bypassed because the initial value of zero is always greater.
- The program reports a negative number of days, which is technically correct but unhelpful.
- The program does not notify the user about the invalid input.
Problems with Negative Values for Apples Sold
- A negative value suggests apples are being lost, which needs to be addressed.
Task Description
- Handle cases where the number of days and apples sold per day are less than zero.
Steps
- Ask Stephen how many days he wants to consider for calculating the total number of apples.
- If the number of days is negative (less than zero), exit the program.
- For each day in the range of days entered by Stephen:
- 2.1: Ask Stephen how many apples were sold.
- 2.2: If the amount is less than zero, return to step 2.1 (repeat until a positive number is entered).
- 2.3: Otherwise, add that amount to the total number of apples sold.
- Report the total number of apples sold across the number of days.
Control Values
- Number of days.
- Amount of apples sold per day.
- Threshold: Zero.
- Operator: Less than.
Implementing the Code
- Similar to the original solution; focus on new code for error handling.
Handling Negative Number of Days
- Use an
if statement to check if num_days is less than zero. - If so:
- Write an error message: "User needs to enter an integral greater than zero."
- Use a
return statement to exit the main method, thus exiting the program.
Dealing with Negative Values for Apples Sold
- Use a
for loop for the number of days. - Need to ensure the user has multiple attempts to enter a valid number of apples sold per day.
Addressing the Problem
- Two problems:
- User must enter daily data greater than zero.
- User must have multiple attempts if the data is invalid.
- Use an
if statement (control variable is the number of apples sold per day, the threshold is zero, the operator is less than) to address the first problem. - Use a
do or while loop to address the second problem (since the number of iterations is unknown).
Implementation using 'do-while' Loop
- Use a
do-while loop because we want to ask the user at least once. - Control variable: output of the current attempt.
- If the attempt is invalid (false), continue the loop until it returns true.
- Threshold:
true with an equality operator.
Code Structure:
- Declare a Boolean variable
isValid (control variable). - Enter the
do loop.- Prompt the user to enter a value for the number of apples sold that day.
- If the number is negative (less than zero), set
isValid to false.- The loop returns to the beginning and asks the user again.
- Otherwise, end the loop and add the number of apples sold per day to the running total.
- Repeat the process for each day.
Testing Invalid Data
- Using invalid cases will increase the number and range of tests required.
- Test cases for the number of days.
- Test cases for the daily number of apples sold:
- User enters one invalid data.
- Multiple invalid data in one day.
- One invalid data in multiple days.
- Multiple invalid data across multiple days.
- Need to redo tests when entering the number of days as zero, to ensure it still functions as expected.
- Add a test just before the boundary (e.g., negative one).
Complexity and Considerations
- Invalid data can lead to different execution paths.
- Invalid data can be caused by user mistakes or issues in the code (e.g., invalid constants).
Emphasis on Invalid Data Handling
- Dealing with invalid data is emphasized in assignments, tutorials, and exams.
- Gradescope highlights when invalid data handling is needed.