forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringToInteger.java
64 lines (55 loc) · 1.92 KB
/
StringToInteger.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.rampatra.strings;
/**
* @author rampatra
* @since 2019-04-01
*/
public class StringToInteger {
/**
* This method converts a {@code String} to an {@code int}. It assumes the {@code string} contains ASCII
* characters only.
*
* @param str the input string, for example, 0, 123, +123, -123, etc.
* @return the equivalent integer.
*/
private static int getIntegerFromString(String str) {
int number = 0;
int digit;
char ch;
int weight = 0;
boolean isNegative = false;
// remove all leading and trailing whitespaces
str = str.trim();
if (str.length() == 0) {
throw new NumberFormatException("Empty string");
}
for (int i = str.length() - 1; i >= 0; i--) {
ch = str.charAt(i);
if (ch == '-' && i == 0) {
isNegative = true;
continue;
} else if (ch == '+' && i == 0) {
continue;
}
digit = ch - '0';
if (digit < 0 || digit > 9) {
throw new NumberFormatException("Invalid characters");
}
number += digit * (Math.pow(10, weight++));
}
return isNegative ? -number : number;
}
public static void main(String[] args) {
// normal cases
System.out.println(getIntegerFromString("0"));
System.out.println(getIntegerFromString("123"));
System.out.println(getIntegerFromString("0123"));
System.out.println(getIntegerFromString("+123"));
System.out.println(getIntegerFromString("-123"));
// error cases
System.out.println(getIntegerFromString("1-23"));
System.out.println(getIntegerFromString(""));
System.out.println(getIntegerFromString(" "));
System.out.println(getIntegerFromString(" "));
System.out.println(getIntegerFromString("123L"));
}
}