
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Program to find whether a string is alphanumeric.
Any word that contains numbers and letters is known as alphanumeric. The following regular expression matches the combination of numbers and letters.
"^[a-zA-Z0-9]+$";
The matches method of the String class accepts a regular expression (in the form of a String) and matches it with the current string in case the match this method returns true else it returns false.
Therefore, to find whether a particular string contains alpha-numeric values −
- Get the string.
- Invoke the match method on it bypassing the above mentioned regular expression.
- Retrieve the result.
Example 1
import java.util.Scanner; public class AlphanumericString { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter input string: "); String input = sc.next(); String regex = "^[a-zA-Z0-9]+$"; boolean result = input.matches(regex); if(result) { System.out.println("Given string is alpha numeric"); } else { System.out.println("Given string is not alpha numeric"); } } }
Output
Enter input string: abc123* Given string is not alpha numeric
Example 2
You can also compile a regular expression and match it with a particular string using the classes and methods (APIs) of the java.util.regex package. The following program is written using these APIs and it verifies whether a given string is alpha-numeric.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main( String args[] ) { Scanner sc = new Scanner(System.in); System.out.println("Enter input string: "); String input = sc.nextLine(); String regex = "^[a-zA-Z0-9]+$"; String data[] = input.split(" "); //Creating a pattern object Pattern pattern = Pattern.compile(regex); for (String ele : data){ //creating a matcher object Matcher matcher = pattern.matcher(ele); if(matcher.matches()) { System.out.println("The word "+ele+": is alpha numeric"); } else { System.out.println("The word "+ele+": is not alpha numeric"); } } } }
Output
Enter input string: hello* this$ is sample text The word hello*: is not alpha numeric The word this$: is not alpha numeric The word is: is alpha numeric The word sample: is alpha numeric The word text: is alpha numeric
- Related Articles
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Alphanumeric Abbreviations of a String in C Program?
- PHP program to remove non-alphanumeric characters from string
- Python Program to accept string ending with alphanumeric character
- Program to find sum of digits that are inside one alphanumeric string in Python
- How to check if a string is alphanumeric in Python?
- Java program to find whether the given string is empty or null
- What is the Python regular expression to check if a string is alphanumeric?
- Java program to print whether the given string is a palindrome
- Haskell Program To Check Whether The Input String Is A Palindrome
- Java program to check whether a given string is Heterogram or not
- Program to check whether a string is subsequence of other in C++
- Java Program to check whether one String is a rotation of another.
- C# program to check whether a given string is Heterogram or not
- Python program to check whether a given string is Heterogram or not

Advertisements