All input data in Java can cause problems in the future. If the input is not verified with the correct type, format, and length, it can cause problems. Some serious security risks are integer errors, buffer overflow, and SQL injections among others. In this article, we’ll show you how to input validation by using a scanner.
Operating Java input validation is to make sure only correct form data has accessed the workflow in an information system, avoiding malformed data existing in the database and caused of various downstream components. We should proceed with Input validation as early as possible in the data flow, we recommend it as soon as the data is received from the external party.
We should access data from all possible untrusted sources before input validation. It includes not only Internet-facing web clients but also backend feeds over extranets, from suppliers, partners, vendors, or regulators. These things may be affected and start sending incorrect data.
You shouldn’t apply Input Validation as the main method of preventing XSS, SQL Injection, and other attacks which are covered in respective cheat sheets but can significantly contribute to reducing their impact if implemented properly.
We can apply hasNextBoolean() method to check if the input is a valid boolean or not and we will use nextBoolean() method to get the input value.
You can input
What is Java input validation?
How to input validation using the Scanner class?
Validate integer input using Scanner in Java
We can use hasNextInt() method to check if the input is an integer and then get that using nextInt() method. You can check the example below. Inputimport java.util.Scanner; public class Main { public static void main(String args[]) { int input = 0; System.out.println("Enter an Integer value "); Scanner sc = new Scanner(System.in); if(sc.hasNextInt()) { input = sc.nextInt(); if(input>0) System.out.println("You entered a positive integer "+input); else { System.out.println("You entered a negative integer "+input); } }else { System.out.println("Please Enter Valid Integer"); } } }Output
Enter an Integer value 50 You entered a positive integer 50
Validate floating point input using Scanner in Java
If you would like to check floating-point values, we recommend hasNextDouble() method and the result will return true if the input is floating type and nextDouble() method is applied to get the user input. You can inputimport java.util.Scanner; public class Main { static Double input; public static void main(String args[]) { System.out.println("Enter a floating point value "); Scanner sc = new Scanner(System.in); if(sc.hasNextDouble()) { input = sc.nextDouble(); if(input>0) System.out.println("You entered a positive value "+input); else { System.out.println("You entered a negative value "+input); } }else { System.out.println("Please Enter a Valid Value"); } } }Output
Enter a floating point value 25.21 You entered a positive value 25.21
Validate Boolean input using Scanner in Java
import java.util.Scanner; public class Main { static Boolean input; public static void main(String args[]) { System.out.println("Enter a boolean value "); Scanner sc = new Scanner(System.in); if(sc.hasNextBoolean()) { input = sc.nextBoolean(); System.out.println("You entered a boolean value "+input); }else { System.out.println("Please Enter a Valid Value"); } } }Output
Enter a boolean value false You entered a boolean value false
Validate String input using Scanner in Java
And if you want to check a string value, you can use regex to get the string in a specific format. The hasNext() takes a regex to validate a string that can consist of only alphabets or you can check the example below. Inputport java.util.Scanner; public class Main { static String input; public static void main(String args[]) { System.out.println("Enter a String "); Scanner sc = new Scanner(System.in); if(sc.hasNext("[A-Za-z]*")) { input = sc.next(); System.out.println("You entered a string value "+input); }else { System.out.println("Please Enter a Valid Value"); } }Output
Enter a String java You entered a string value java
Conclusion
In this article, we’ve mentioned, “How to input validation by using the Scanner class”. If you would like to share more or have more ideas but you’re not sure where you should start, please contact us. We – ONEXTDEGITAL will provide Web and Mobile App Development with the best price and the latest technology.>Read more