Check if a String is numeric in Java

0 votes
Check a String if it is a number before we parse the string.
Apr 16, 2018 in Java by parth295
• 4,640 points
817 views

3 answers to this question.

0 votes
  1. With Apache Commons Lang 3.4 and below: NumberUtils.isNumber or StringUtils.isNumeric.
  2. While using Apache Commons Lang 3.5 and above: NumberUtils.isCreatable or StringUtils.isNumeric.
  3. You can also use StringUtils.isNumericSpace which returns true for empty strings and ignores internal spaces in the string
answered Apr 16, 2018 by developer_1
• 3,300 points
0 votes

This is the fastest way i know to check if String is Number or not:

public static boolean isNumber(String str){
  int i=0, len=str.length();
  boolean a=false,b=false,c=false, d=false;
  if(i<len && (str.charAt(i)=='+' || str.charAt(i)=='-')) i++;
  while( i<len && isDigit(str.charAt(i)) ){ i++; a=true; }
  if(i<len && (str.charAt(i)=='.')) i++;
  while( i<len && isDigit(str.charAt(i)) ){ i++; b=true; }
  if(i<len && (str.charAt(i)=='e' || str.charAt(i)=='E') && (a || b)){ i++; c=true; }
  if(i<len && (str.charAt(i)=='+' || str.charAt(i)=='-') && c) i++;
  while( i<len && isDigit(str.charAt(i)) ){ i++; d=true;}
  return i==len && (a||b) && (!c || (c && d));
}
static boolean isDigit(char c){
  return c=='0' || c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9';
}
answered Aug 2, 2018 by samarth295
• 2,190 points
0 votes

Java 8 Lambda Expression is used:

String someString = "123123";
boolean isNumeric = someString.chars().allMatch( Character::isDigit );
answered Sep 3, 2018 by Daisy
• 8,050 points

Related Questions In Java

0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,050 points
222 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19 in Java by Sirajul
• 37,480 points
91 views
+1 vote
1 answer

What is a simple way to repeat a string in java?

From Java 11 on, there's a method String::repeat that does ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,810 points
858 views
0 votes
1 answer
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,350 points
88 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 37,480 points
187 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,050 points
136 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
64 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19 in Java by Sirajul
• 37,480 points
71 views
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19 in Java by Sirajul
• 37,480 points
76 views