Sunday 5 April 2020

How to check given string represent integer or not?

Approach 1: Using Integer.parseInt method
public static boolean isInteger_ByParseInt(String str) {
         if (str == null || str.isEmpty()) {
                  return false;
         }

         try {
                  Integer.parseInt(str);
                  return true;
         } catch (NumberFormatException e) {
                  return false;
         }
}

Approach 2: Using regular expression
public static boolean isInteger_ByRegEx(String str) {
         if (str == null || str.isEmpty()) {
                  return false;
         }

         return str.matches("^-?\\d+$");
}

Approach 3: Check each characer whether it is digit or not.
public static boolean isInteger_ByCharacterCheck(String str) {
         if (str == null || str.isEmpty()) {
                  return false;
         }

         int i = 0;
         if (str.charAt(0) == '-') {
                  if (str.length() == 1) {
                           return false;
                  }

                  i = 1;

         }

         for (; i < str.length(); i++) {
                  if (!Character.isDigit(str.charAt(i))) {
                           return false;
                  }
         }

         return true;
}

StringUtil.java
package com.smaple.app.utils;

public class StringUtil {
 public static boolean isInteger_ByParseInt(String str) {
  if (str == null || str.isEmpty()) {
   return false;
  }

  try {
   Integer.parseInt(str);
   return true;
  } catch (NumberFormatException e) {
   return false;
  }
 }

 public static boolean isInteger_ByRegEx(String str) {
  if (str == null || str.isEmpty()) {
   return false;
  }

  return str.matches("^-?\\d+$");
 }

 public static boolean isInteger_ByCharacterCheck(String str) {
  if (str == null || str.isEmpty()) {
   return false;
  }

  int i = 0;
  if (str.charAt(0) == '-') {
   if (str.length() == 1) {
    return false;
   }

   i = 1;

  }

  for (; i < str.length(); i++) {
   if (!Character.isDigit(str.charAt(i))) {
    return false;
   }
  }

  return true;
 }

}

StringUtilTest.java
package com.sample.app.utils;

import static com.smaple.app.utils.StringUtil.isInteger_ByCharacterCheck;
import static com.smaple.app.utils.StringUtil.isInteger_ByParseInt;
import static com.smaple.app.utils.StringUtil.isInteger_ByRegEx;
import static org.junit.Assert.*;

import org.junit.Test;

public class StringUtilTest {

 @Test
 public void emptyString() {
  String str1 = "";
  String str2 = " ";
  String str3 = "  ";

  assertFalse(isInteger_ByParseInt(str1));
  assertFalse(isInteger_ByParseInt(str2));
  assertFalse(isInteger_ByParseInt(str3));

  assertFalse(isInteger_ByRegEx(str1));
  assertFalse(isInteger_ByRegEx(str2));
  assertFalse(isInteger_ByRegEx(str3));

  assertFalse(isInteger_ByCharacterCheck(str1));
  assertFalse(isInteger_ByCharacterCheck(str2));
  assertFalse(isInteger_ByCharacterCheck(str3));
 }

 @Test
 public void nullString() {
  String str1 = null;

  assertFalse(isInteger_ByParseInt(str1));
  assertFalse(isInteger_ByRegEx(str1));
  assertFalse(isInteger_ByCharacterCheck(str1));

 }

 @Test
 public void positiveNumber() {
  String str1 = "1";
  String str2 = "123";

  assertTrue(isInteger_ByParseInt(str1));
  assertTrue(isInteger_ByRegEx(str1));
  assertTrue(isInteger_ByCharacterCheck(str1));

  assertTrue(isInteger_ByParseInt(str2));
  assertTrue(isInteger_ByRegEx(str2));
  assertTrue(isInteger_ByCharacterCheck(str2));

 }

 @Test
 public void negativeNumber() {
  String str1 = "-1";
  String str2 = "-123";

  assertTrue(isInteger_ByParseInt(str1));
  assertTrue(isInteger_ByRegEx(str1));
  assertTrue(isInteger_ByCharacterCheck(str1));

  assertTrue(isInteger_ByParseInt(str2));
  assertTrue(isInteger_ByRegEx(str2));
  assertTrue(isInteger_ByCharacterCheck(str2));

 }

 @Test
 public void zero() {
  String str1 = "0";

  assertTrue(isInteger_ByParseInt(str1));
  assertTrue(isInteger_ByRegEx(str1));
  assertTrue(isInteger_ByCharacterCheck(str1));

 }
}

Note
There is a problem with approach 1 (Using Integer.parseInt() method), it only works when Integer is in range (Between Integer.MIN_VALUE and Integer.MAX_VALUE), If the integer out of range, then this approach1 fails.

@Test
public void integerOutOfRange() {
         String str1 = Integer.MAX_VALUE + "1";
         assertFalse(isInteger_ByParseInt(str1));
         assertTrue(isInteger_ByRegEx(str1));
         assertTrue(isInteger_ByCharacterCheck(str1));
}

You may like

No comments:

Post a Comment