Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will oftentimes need to validate user-input in programs - only allowing the

ID: 3593697 • Letter: Y

Question

You will oftentimes need to validate user-input in programs - only allowing the user to enter specific types of input and/or forcing them to reenter if this input is incorrect. For example, let’s say you have a menu with choices A, B, and C, but the user decides to enter W. Using loops, you may now tell the user that this is incorrect and force them to make another choice (hopefully a correct one this time).

To practice with this, write a program that asks the user to enter a positive integer. If that number is negative, re ask the same question. Now add one more criteria: the number must also not be divisible by 10 (once again, if it is, repeat the original question). Once a correct number has been inputted by the user, have the program display their number along with the message “Wise choice.”

Explanation / Answer

Validation.java

import java.util.Scanner;

public class Validation {

public static void main(String[] args) {

// Declaring variables

int num;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

/*

* This while loop continues to execute until the user enters a valid

* number

*/

while (true) {

// Getting the input entered by the user

System.out.print("Enter a Positive Number :");

num = sc.nextInt();

// Checking whether the number is positive or not

if (num < 0) {

System.out.println("** Invalid.Number Must be Positive **");

continue;

} else if (num % 10 != 0)// Checking whether the number is divisible

// by 10 or not

{

System.out

.println("** Invalid.Number is not divisible by 10 **");

continue;

} else {

System.out.print(num + " Wise Choice");

break;

}

}

}

}

_______________

Output:

Enter a Positive Number :-45
** Invalid.Number Must be Positive **
Enter a Positive Number :45
** Invalid.Number is not divisible by 10 **
Enter a Positive Number :450
450 Wise Choice

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote