Skip to content

Commit 07838a2

Browse files
ramansahasimaibin
authored andcommitted
BAEL-1739 - Check If a String is Numeric in Java (eugenp#4209)
* BAEL-1725 Java Pass-by-reference vs Pass-by-value - First commit * updated test cases * First commit consisting only of test cases * First commit with test cases * Introduced JMH Michrobenchmarking * Minor corrections
1 parent 387c325 commit 07838a2

10 files changed

Lines changed: 297 additions & 0 deletions
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.BenchmarkMode;
7+
import org.openjdk.jmh.annotations.Mode;
8+
import org.openjdk.jmh.annotations.OutputTimeUnit;
9+
import org.openjdk.jmh.annotations.Scope;
10+
import org.openjdk.jmh.annotations.State;
11+
import org.openjdk.jmh.runner.Runner;
12+
import org.openjdk.jmh.runner.RunnerException;
13+
import org.openjdk.jmh.runner.options.Options;
14+
import org.openjdk.jmh.runner.options.OptionsBuilder;
15+
16+
17+
public class Benchmarking {
18+
public static void main(String[] args) throws RunnerException {
19+
Options opt = new OptionsBuilder()
20+
.include(Benchmarking.class.getSimpleName())
21+
.forks(1)
22+
.build();
23+
24+
new Runner(opt).run();
25+
}
26+
27+
@State(Scope.Thread)
28+
public static class ExecutionPlan {
29+
public String number = Integer.toString(Integer.MAX_VALUE);
30+
public boolean isNumber = false;
31+
public IsNumeric isNumeric= new IsNumeric();
32+
}
33+
34+
@Benchmark
35+
@BenchmarkMode(Mode.AverageTime)
36+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
37+
public void usingCoreJava(ExecutionPlan plan) {
38+
plan.isNumber = plan.isNumeric.usingCoreJava(plan.number);
39+
}
40+
41+
@Benchmark
42+
@BenchmarkMode(Mode.AverageTime)
43+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
44+
public void usingRegularExpressions(ExecutionPlan plan) {
45+
plan.isNumber = plan.isNumeric.usingRegularExpressions(plan.number);
46+
}
47+
48+
@Benchmark
49+
@BenchmarkMode(Mode.AverageTime)
50+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
51+
public void usingNumberUtils_isCreatable(ExecutionPlan plan) {
52+
plan.isNumber = plan.isNumeric.usingNumberUtils_isCreatable(plan.number);
53+
}
54+
55+
@Benchmark
56+
@BenchmarkMode(Mode.AverageTime)
57+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
58+
public void usingNumberUtils_isParsable(ExecutionPlan plan) {
59+
plan.isNumber = plan.isNumeric.usingNumberUtils_isParsable(plan.number);
60+
}
61+
62+
@Benchmark
63+
@BenchmarkMode(Mode.AverageTime)
64+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
65+
public void usingStringUtils_isNumeric(ExecutionPlan plan) {
66+
plan.isNumber = plan.isNumeric.usingStringUtils_isNumeric(plan.number);
67+
}
68+
69+
@Benchmark
70+
@BenchmarkMode(Mode.AverageTime)
71+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
72+
public void usingStringUtils_isNumericSpace(ExecutionPlan plan) {
73+
plan.isNumber = plan.isNumeric.usingStringUtils_isNumericSpace(plan.number);
74+
}
75+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.apache.commons.lang3.math.NumberUtils;
5+
6+
public class IsNumeric {
7+
public boolean usingCoreJava(String strNum) {
8+
try {
9+
double d = Double.parseDouble(strNum);
10+
} catch (NumberFormatException | NullPointerException nfe) {
11+
return false;
12+
}
13+
return true;
14+
}
15+
16+
public boolean usingRegularExpressions(String strNum) {
17+
return strNum.matches("-?\\d+(\\.\\d+)?");
18+
}
19+
20+
public boolean usingNumberUtils_isCreatable(String strNum) {
21+
return NumberUtils.isCreatable(strNum);
22+
}
23+
24+
public boolean usingNumberUtils_isParsable(String strNum) {
25+
return NumberUtils.isParsable(strNum);
26+
}
27+
28+
public boolean usingStringUtils_isNumeric(String strNum) {
29+
return StringUtils.isNumeric(strNum);
30+
}
31+
32+
public boolean usingStringUtils_isNumericSpace(String strNum) {
33+
return StringUtils.isNumericSpace(strNum);
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4+
5+
public class IsNumericDriver {
6+
private static IsNumeric isNumeric;
7+
8+
static {
9+
isNumeric =new IsNumeric();
10+
}
11+
12+
public static void main(String[] args) {
13+
LOG.info("Testing all methods...");
14+
15+
boolean res = isNumeric.usingCoreJava("1001");
16+
LOG.info("Using Core Java : " + res);
17+
18+
res = isNumeric.usingRegularExpressions("1001");
19+
LOG.info("Using Regular Expressions : " + res);
20+
21+
res =isNumeric.usingNumberUtils_isCreatable("1001");
22+
LOG.info("Using NumberUtils.isCreatable : " + res);
23+
24+
res =isNumeric.usingNumberUtils_isParsable("1001");
25+
LOG.info("Using NumberUtils.isParsable : " + res);
26+
27+
res =isNumeric.usingStringUtils_isNumeric("1001");
28+
LOG.info("Using StringUtils.isNumeric : " + res);
29+
30+
res =isNumeric.usingStringUtils_isNumericSpace("1001");
31+
LOG.info("Using StringUtils.isNumericSpace : " + res);
32+
}
33+
}
3.15 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class CoreJavaIsNumericUnitTest {
8+
public static boolean isNumeric(String strNum) {
9+
try {
10+
double d = Double.parseDouble(strNum);
11+
} catch (NumberFormatException | NullPointerException nfe) {
12+
return false;
13+
}
14+
return true;
15+
}
16+
17+
@Test
18+
public void whenUsingCoreJava_thenTrue() {
19+
// Valid Numbers
20+
assertThat(isNumeric("22")).isTrue();
21+
assertThat(isNumeric("5.05")).isTrue();
22+
assertThat(isNumeric("-200")).isTrue();
23+
assertThat(isNumeric("10.0d")).isTrue();
24+
assertThat(isNumeric(" 22 ")).isTrue();
25+
26+
// Invalid Numbers
27+
assertThat(isNumeric(null)).isFalse();
28+
assertThat(isNumeric("")).isFalse();
29+
assertThat(isNumeric("abc")).isFalse();
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.apache.commons.lang3.math.NumberUtils;
6+
import org.junit.Test;
7+
8+
public class NumberUtilsIsCreatableUnitTest {
9+
@Test
10+
public void givenApacheCommons_whenUsingIsParsable_thenTrue() {
11+
// Valid Numbers
12+
assertThat(NumberUtils.isCreatable("22")).isTrue();
13+
assertThat(NumberUtils.isCreatable("5.05")).isTrue();
14+
assertThat(NumberUtils.isCreatable("-200")).isTrue();
15+
assertThat(NumberUtils.isCreatable("10.0d")).isTrue();
16+
assertThat(NumberUtils.isCreatable("1000L")).isTrue();
17+
assertThat(NumberUtils.isCreatable("0xFF")).isTrue();
18+
assertThat(NumberUtils.isCreatable("07")).isTrue();
19+
assertThat(NumberUtils.isCreatable("2.99e+8")).isTrue();
20+
21+
// Invalid Numbers
22+
assertThat(NumberUtils.isCreatable(null)).isFalse();
23+
assertThat(NumberUtils.isCreatable("")).isFalse();
24+
assertThat(NumberUtils.isCreatable("abc")).isFalse();
25+
assertThat(NumberUtils.isCreatable(" 22 ")).isFalse();
26+
assertThat(NumberUtils.isCreatable("09")).isFalse();
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.apache.commons.lang3.math.NumberUtils;
6+
import org.junit.Test;
7+
8+
public class NumberUtilsIsParsableUnitTest {
9+
@Test
10+
public void givenApacheCommons_whenUsingIsParsable_thenTrue() {
11+
// Valid Numbers
12+
assertThat(NumberUtils.isParsable("22")).isTrue();
13+
assertThat(NumberUtils.isParsable("-23")).isTrue();
14+
assertThat(NumberUtils.isParsable("2.2")).isTrue();
15+
assertThat(NumberUtils.isParsable("09")).isTrue();
16+
17+
// Invalid Numbers
18+
assertThat(NumberUtils.isParsable(null)).isFalse();
19+
assertThat(NumberUtils.isParsable("")).isFalse();
20+
assertThat(NumberUtils.isParsable("6.2f")).isFalse();
21+
assertThat(NumberUtils.isParsable("9.8d")).isFalse();
22+
assertThat(NumberUtils.isParsable("22L")).isFalse();
23+
assertThat(NumberUtils.isParsable("0xFF")).isFalse();
24+
assertThat(NumberUtils.isParsable("2.99e+8")).isFalse();
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class RegularExpressionsUnitTest {
8+
public static boolean isNumeric(String strNum) {
9+
return strNum.matches("-?\\d+(\\.\\d+)?");
10+
}
11+
12+
@Test
13+
public void whenUsingRegularExpressions_thenTrue() {
14+
// Valid Numbers
15+
assertThat(isNumeric("22")).isTrue();
16+
assertThat(isNumeric("5.05")).isTrue();
17+
assertThat(isNumeric("-200")).isTrue();
18+
19+
// Invalid Numbers
20+
assertThat(isNumeric("abc")).isFalse();
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.junit.Test;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class StringUtilsIsNumericSpaceUnitTest {
8+
@Test
9+
public void givenApacheCommons_whenUsingIsNumericSpace_thenTrue() {
10+
// Valid Numbers
11+
assertThat(StringUtils.isNumericSpace("123")).isTrue();
12+
assertThat(StringUtils.isNumericSpace("١٢٣")).isTrue();
13+
assertThat(StringUtils.isNumericSpace("")).isTrue();
14+
assertThat(StringUtils.isNumericSpace(" ")).isTrue();
15+
assertThat(StringUtils.isNumericSpace("12 3")).isTrue();
16+
17+
// Invalid Numbers
18+
assertThat(StringUtils.isNumericSpace(null)).isFalse();
19+
assertThat(StringUtils.isNumericSpace("ab2c")).isFalse();
20+
assertThat(StringUtils.isNumericSpace("12.3")).isFalse();
21+
assertThat(StringUtils.isNumericSpace("-123")).isFalse();
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.stringisnumeric;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.junit.Test;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class StringUtilsIsNumericUnitTest {
8+
@Test
9+
public void givenApacheCommons_whenUsingIsNumeric_thenTrue() {
10+
// Valid Numbers
11+
assertThat(StringUtils.isNumeric("123")).isTrue();
12+
assertThat(StringUtils.isNumeric("١٢٣")).isTrue();
13+
assertThat(StringUtils.isNumeric("१२३")).isTrue();
14+
15+
// Invalid Numbers
16+
assertThat(StringUtils.isNumeric(null)).isFalse();
17+
assertThat(StringUtils.isNumeric("")).isFalse();
18+
assertThat(StringUtils.isNumeric(" ")).isFalse();
19+
assertThat(StringUtils.isNumeric("12 3")).isFalse();
20+
assertThat(StringUtils.isNumeric("ab2c")).isFalse();
21+
assertThat(StringUtils.isNumeric("12.3")).isFalse();
22+
assertThat(StringUtils.isNumeric("-123")).isFalse();
23+
}
24+
}

0 commit comments

Comments
 (0)