Skip to content

Commit 176b5a8

Browse files
committed
Parsing integers: 90% done
1 parent 89667bd commit 176b5a8

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.rampatra.strings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019-04-01
6+
*/
7+
public class IntegerToString {
8+
9+
private static final int[] sizeTable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999,
10+
999999999, Integer.MAX_VALUE};
11+
12+
private static String getStringFromInteger(int num) {
13+
boolean isNegative = num < 0;
14+
num = isNegative ? -num : num;
15+
int size = getStringSize(num);
16+
size = isNegative ? size + 1 : size;
17+
char[] chars = new char[size];
18+
19+
int rem;
20+
for (int i = size - 1; isNegative ? i > 0 : i >= 0; i--) {
21+
rem = num % 10;
22+
num = num / 10;
23+
chars[i] = (char) (rem + '0');
24+
}
25+
26+
if (isNegative) {
27+
chars[0] = '-';
28+
}
29+
30+
return new String(chars);
31+
}
32+
33+
private static int getStringSize(int num) {
34+
if (num == Integer.MAX_VALUE) return 10;
35+
36+
for (int i = 0; ; i++) {
37+
if (num < sizeTable[i]) {
38+
return i + 1;
39+
}
40+
}
41+
}
42+
43+
public static void main(String[] args) {
44+
System.out.println(getStringFromInteger(0));
45+
System.out.println(getStringFromInteger(123));
46+
System.out.println(getStringFromInteger(+123));
47+
System.out.println(getStringFromInteger(-123));
48+
System.out.println(getStringFromInteger(Integer.MAX_VALUE));
49+
System.out.println(getStringFromInteger(Integer.MIN_VALUE)); // not working
50+
}
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.rampatra.strings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019-04-01
6+
*/
7+
public class StringToInteger {
8+
9+
/**
10+
* This method converts a {@code String} to an {@code int}. It assumes the {@code string} contains ASCII
11+
* characters only.
12+
*
13+
* @param str the input string, for example, 0, 123, +123, -123, etc.
14+
* @return the equivalent integer.
15+
*/
16+
private static int getIntegerFromString(String str) {
17+
int number = 0;
18+
int digit;
19+
char ch;
20+
int weight = 0;
21+
boolean isNegative = false;
22+
23+
// remove all leading and trailing whitespaces
24+
str = str.trim();
25+
if (str.length() == 0) {
26+
throw new NumberFormatException("Empty string");
27+
}
28+
29+
for (int i = str.length() - 1; i >= 0; i--) {
30+
ch = str.charAt(i);
31+
if (ch == '-' && i == 0) {
32+
isNegative = true;
33+
continue;
34+
} else if (ch == '+' && i == 0) {
35+
continue;
36+
}
37+
38+
digit = ch - '0';
39+
40+
if (digit < 0 || digit > 9) {
41+
throw new NumberFormatException("Invalid characters");
42+
}
43+
44+
number += digit * (Math.pow(10, weight++));
45+
}
46+
return isNegative ? -number : number;
47+
}
48+
49+
public static void main(String[] args) {
50+
// normal cases
51+
System.out.println(getIntegerFromString("0"));
52+
System.out.println(getIntegerFromString("123"));
53+
System.out.println(getIntegerFromString("0123"));
54+
System.out.println(getIntegerFromString("+123"));
55+
System.out.println(getIntegerFromString("-123"));
56+
57+
// error cases
58+
System.out.println(getIntegerFromString("1-23"));
59+
System.out.println(getIntegerFromString(""));
60+
System.out.println(getIntegerFromString(" "));
61+
System.out.println(getIntegerFromString(" "));
62+
System.out.println(getIntegerFromString("123L"));
63+
}
64+
}

0 commit comments

Comments
 (0)