Core Java Coding Interview Question -- validating an input String to be alphanumeric
Q. Can you write sample code as to different ways in which you can validate an input String to return true if the input is alpha numeric and otherwise return false?
A. It can be done a number of different ways.
- Using regex.
- Without using regex.
Step 1: Have the right dependency jars in the pom.xml file as shown below.
<dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>
Step 2: The implementation class.
package com.mycompany.app5; import org.apache.commons.lang.StringUtils; public class AlphaNumeric { public boolean isAlphaNumeric(String input) { //fail fast if (StringUtils.isEmpty(input)) { throw new IllegalArgumentException("The input cannot be empty !!!"); } boolean result = false; if (input.matches("[a-zA-Z0-9]+")) // one or more alpha numeric characters { result = true; } return result; } }
Step 3:The JUnit test class.
package com.mycompany.app5; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; public class AlphaNumericTest { AlphaNumeric an; @Before public void setUp() { an = new AlphaNumeric(); } @Test public void testAlphaNumericHappyPath() { Assert.assertEquals(true, an.isAlphaNumeric("AUD123")); Assert.assertEquals(true, an.isAlphaNumeric("123aud")); Assert.assertEquals(true, an.isAlphaNumeric("123")); Assert.assertEquals(true, an.isAlphaNumeric("hgD")); Assert.assertEquals(false, an.isAlphaNumeric("hg*D")); Assert.assertEquals(false, an.isAlphaNumeric("@hgD")); Assert.assertEquals(false, an.isAlphaNumeric("@hgD")); Assert.assertEquals(false, an.isAlphaNumeric("@*#")); } @Test(expected = java.lang.IllegalArgumentException.class) public void testAlphaNumericUnHappyPath1() { Assert.assertEquals(true, an.isAlphaNumeric(null)); } @Test(expected = java.lang.IllegalArgumentException.class) public void testAlphaNumericUnHappyPath2() { Assert.assertEquals(true, an.isAlphaNumeric("")); } }Using apache's commons-lang package's method directly as shown below.
package com.mycompany.app5; import org.apache.commons.lang.StringUtils; public class AlphaNumeric { public boolean isAlphaNumeric(String input) { //fail fast if (StringUtils.isEmpty(input)) { throw new IllegalArgumentException("The input cannot be empty !!!"); } boolean result = false; if (StringUtils.isAlphanumeric(input)) // one or more alpha numeric characters { result = true; } return result; } }Using the core Java without any framework
package com.mycompany.app5; import org.apache.commons.lang.StringUtils; public class AlphaNumeric { public boolean isAlphaNumeric(String input) { //fail fast if (StringUtils.isEmpty(input)) { throw new IllegalArgumentException("The input cannot be empty !!!"); } boolean result = true; //chack each character agaianst the ASCII table hex value for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a) { result = false; break; } } return result; } }Alternatively, using the Character class.
package com.mycompany.app5; import org.apache.commons.lang.StringUtils; public class AlphaNumeric { public boolean isAlphaNumeric(String input) { //fail fast if (StringUtils.isEmpty(input)) { throw new IllegalArgumentException("The input cannot be empty !!!"); } boolean result = true; //chack each character agaianst the ASCII table hex value for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (!Character.isDigit(c) && !Character.isLetter(c)) { result = false; break; } } return result; } }
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home