Why is input validation importantSolutionwe have to give the

Why is input validation important?

Solution

we have to give the valid input input to the function of methgod.so that It will calcluate the give us the result.If we didnt supply the valid input to the function we may get wrong output.

To avoid this first we have to validate the inputs entered by the user.after getting the input from the user we have to write a condition to validate the input.then we have to pass that user entered input to that condition .if that condition fails we have to prompt user again to enter the valid input.This process continues till the user enteres a valid input.

Example

Demo.java

import java.util.Scanner;

public class Demo {

   public static void main(String[] args) {
int a,b;
       Scanner sc=new Scanner(System.in);
       System.out.println(\"** We are performing Division of two numbers ** \");

           System.out.print(\"Enter first number :\");
           a=sc.nextInt();

  
           System.out.print(\"Enter second number :\");
           b=sc.nextInt();
      

      
       float division=(float)a/b;
       System.out.println(\"The result when \"+a+\" divided with \"+b+\" is \"+division);
          
      
   }

}

__________________________

Output:

** We are performing Division of two numbers **
Enter first number :25
Enter second number :0
The result when 25 divided with 0 is Infinity

___________________________

Demo.java

import java.util.Scanner;

public class Demo {

   public static void main(String[] args) {
int a,b;
       Scanner sc=new Scanner(System.in);
       System.out.println(\"** We are performing Division of two numbers ** \");

           System.out.print(\"Enter first number :\");
           a=sc.nextInt();

       while(true)
       {
           System.out.print(\"Enter second number :\");
           b=sc.nextInt();
           if(b==0)
           {
               System.out.println(\":: Invalid Input.Number must not be zero ::\");
               continue;
           }
           else
               break;
       }
      
       float division=(float)a/b;
       System.out.println(\"The result when \"+a+\" divided with \"+b+\" is \"+division);
          
      
   }

}

________________

Output:

** We are performing Division of two numbers **
Enter first number :25
Enter second number :0
:: Invalid Input.Number must not be zero ::
Enter second number :5
The result when 25 divided with 5 is 5.0

____________________

Hee in this division if the denominator is zero we cant perform the division operation.So we are validating the user input.If the user enters zero ,we have to prompt the user again to enter the input until the user enters a valid input.

____________Thank You

Why is input validation important?Solutionwe have to give the valid input input to the function of methgod.so that It will calcluate the give us the result.If w
Why is input validation important?Solutionwe have to give the valid input input to the function of methgod.so that It will calcluate the give us the result.If w

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site