-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStringToLong.java
More file actions
43 lines (40 loc) · 1.2 KB
/
Copy pathStringToLong.java
File metadata and controls
43 lines (40 loc) · 1.2 KB
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
/**
*
* @author Luca Graziani
*
* This class contains a method to cast from string to long
* without using the java cast
*/
public class StringToLong {
private long stringToLong(String input) {
long maxValue = Long.MAX_VALUE;
// The maximum number stored in a Long is 19 digits
if(input.length()>19){
System.out.println("The number is too big, The maximum number is " + maxValue);
return -1;
}
long result = 0;
String digits = "0123456789";
for (int i = 0; i <= input.length()-1; i++) {
char c = (char) input.charAt(i);
int lastValue = digits.indexOf(c);
// In the string there is a characters that is not a number
if(lastValue < 0){
System.out.println("Invalid charater in the string" + lastValue);
return -1;
}
if(result*10 + lastValue <= maxValue){
result = result*10 + lastValue;
}else{
// The number is bigger than the maximum supported by a Long
System.out.println("The number is too big, The maximum number is " + maxValue);
return -1;
}
}
return result;
}
public static void main(String args[]){
StringToLong test = new StringToLong();
System.out.print(test.stringToLong("23425356436"));
}
}