Answered step by step
Verified Expert Solution
Question
1 Approved Answer
E Ensign Homepage C D Q & O 19 LDS 28 63F Clear FAFSA Student Loan ck Credit Karma M Gmail 2022 Fall Semester
E Ensign Homepage C D Q & O 19 LDS 28 63F Clear FAFSA Student Loan ck Credit Karma M Gmail 2022 Fall Semester Home 5.4 Lab-Input Validation (1 hr. 30 X ensign.instructure.com/courses/14050/assignments/798604?module_item_id=1612087 Announcements Syllabus Modules Grades People X Tutoring Resources Course Hero iCloud is byu chat page TestOut LabSim X Package Name: week5 . Class Name: InputValidationInteger When submitting this lab, submit a java file called "InputValidationInteger", and create the following structure in Eclipse: + } while (!valid); Step 7: Ponder and discuss with your partner the following: Course Hero Write a program that asks the user to enter a whole number. Use input validation to ensure the user entered a valid whole number. Perform simple math on the number to ensure your input validation routine worked as expected. Display the results. Step 1: Create a class, InputValidationInteger with a main() method. Step 2: Declare a Scanner object, in. Step 3: Declare a String variable, input, and initialize it to "". Step 4: Declare three integer variables num1, num2, and num3 and initialize them to 0. Step 5: Declare a boolean control variable, valid, and set it to false. Step 6: Create a do-while loop using the valid control variable. do { a I O 6:47 PM 10/11/2022 X : E Ensign Homepage C D Q & O 19 LDS 28 63F Clear FAFSA Student Loan ck Credit Karma M Gmail 2022 Fall Semester Home 5.4 Lab-Input Validation (1 hr. 30 X ensign.instructure.com/courses/14050/assignments/798604?module_item_id=1612087 Announcements Syllabus Modules Grades People X Tutoring Resources do{ Course Hero iCloud is byu chat page The program should now look something like this: //Declare variables } catch (Exception e) { Step 8: In the body of the do-while loop, prompt the user to enter a number. Step 9: Use in.nextLine() to get the input from the user. Step 10: Create a try/catch statement inside your while loop. Use the standard Exception to catch all exceptions in the catch statement. System.out.print("Please enter a number: "); input = in.nextLine(); try{ }//end of try/catch }while (!valid}; X TestOut LabSim + Course Hero a I O 6:48 PM 10/11/2022 X : E Ensign Homepage C D Q & O 19 LDS 28 63F Clear FAFSA Student Loan ck Credit Karma M Gmail 2022 Fall Semester Home 5.4 Lab-Input Validation (1 hr. 30 X ensign.instructure.com/courses/14050/assignments/798604?module_item_id=1612087 Announcements Syllabus Modules Grades People X Tutoring Resources Course Hero iCloud s byu chat page Example Run X Step 11: In the try block, convert the user's input into an integer and store it in num1 using Integer.parseInt(). Please enter a number: Pizza Invalid Response! Please enter a whole number. Please enter a number: Uno TestOut LabSim Step 12: If the parseInt() statement ran without issue, then you can set valid = true so that the program will exit the do-while loop once a valid integer has been entered. Add the following statement to the try block: valid = true; Step 13: If the user entered something other than a valid integer, the code in the catch block will run without crashing the program. Display a helpful message prompting the user to correct the error inside the catch block: + System.out.println("Error: Please enter a whole number. "); Step 14: Outside of the do-while loop, perform the following mathematical calculations: Set num2 to equal num1 + 1000. Set num3 to equal num1 * 2. Step 15: Print out num1, num2, and num3 as shown in the example run. Course Hero a I O 6:48 PM 10/11/2022 X : E Ensign Homepage C D Q & O 19 LDS 28 63F Clear FAFSA Student Loan ck Credit Karma M Gmail 2022 Fall Semester Home 5.4 Lab-Input Validation (1 hr. 30 X ensign.instructure.com/courses/14050/assignments/798604?module_item_id=1612087 Announcements Syllabus Modules Grades People X Tutoring Resources Example Run Course Hero iCloud is byu chat page Please enter a number: Pizza Invalid Response! Please enter a whole number. Please enter a number: Uno Invalid Response! Please enter a whole number. Please enter a number: 42 num1 = 42. num2 = 1042. num3 = 84. Step 13: If the user entered something other than a valid integer, the code in the catch block will run without crashing the program. Display a helpful message prompting the user to correct the error inside the catch block: System.out.println("Error: Please enter a whole number. "); Step 14: Outside of the do-while loop, perform the following mathematical calculations: Set num2 to equal num1 + 1000. Set num3 to equal num1 * 2. Step 15: Print out num1, num2, and num3 as shown in the example run. X TestOut LabSim + Course Hero a I O 6:48 PM 10/11/2022 X :
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started