-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathParseInteger.java
46 lines (40 loc) · 1.43 KB
/
ParseInteger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.thealgorithms.maths;
public final class ParseInteger {
private ParseInteger() {
}
private static void checkInput(final String s) {
if (s == null) {
throw new NumberFormatException("Input parameter must not be null!");
}
if (s.isEmpty()) {
throw new NumberFormatException("Input parameter must not be empty!");
}
}
private static void checkDigitAt(final String s, final int pos) {
if (!Character.isDigit(s.charAt(pos))) {
throw new NumberFormatException("Input parameter of incorrect format: " + s);
}
}
private static int digitToInt(final char digit) {
return digit - '0';
}
/**
* Parse a string to integer
*
* @param s the string
* @return the integer value represented by the argument in decimal.
* @throws NumberFormatException if the {@code string} does not contain a
* parsable integer.
*/
public static int parseInt(final String s) {
checkInput(s);
final boolean isNegative = s.charAt(0) == '-';
final boolean isPositive = s.charAt(0) == '+';
int number = 0;
for (int i = isNegative || isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
checkDigitAt(s, i);
number = number * 10 + digitToInt(s.charAt(i));
}
return isNegative ? -number : number;
}
}