forked from patilankita79/CopartChallenge_April4_2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOperations.java
More file actions
39 lines (32 loc) · 1.14 KB
/
NumberOperations.java
File metadata and controls
39 lines (32 loc) · 1.14 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
/**
* Created by Lakshmi on 4/4/2017.
* Problem: Convert String to Integer (Simple) (10 points)
*/
public class NumberOperations {
public static void main(String[] args) {
// Set input from another function or from arguments
String input = "431";
// Call getInteger function
int result = getInteger(input);
System.out.println(result);
}
// Build integer from string
static int getInteger(String input){
// String to map from char to integer through string index value
String numMap = "0123456789";
// Initializations
int result = 0;
int size = input.length();
// Loop through input string
for (int i = size; i > 0; i -= 1) {
// Get char that has to be converted
char numberC = input.charAt(size - i);
// Convert char to int
int numberI = numMap.indexOf(numberC);
// Assign correct decimal place before adding to result
int factor = (int)Math.pow(10, i - 1);
result += factor * numberI;
}
return result;
}
}