-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerEquivalent.java
More file actions
40 lines (38 loc) · 1.32 KB
/
Copy pathIntegerEquivalent.java
File metadata and controls
40 lines (38 loc) · 1.32 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
public class IntegerEquivalent {
public static void main (String[] args) {
System.out.println(integerEquivalent(new char[] {'1', '2', '3', '1'}));
System.out.println(integerEquivalent(new char[] {'1','3','9'}));
System.out.println(integerEquivalent(new char[] {'0','3', '1'}));
System.out.println(integerEquivalent(new char[] {'1', '+', '5'}));
System.out.println(integerEquivalent(new char[] {'1', '5', '+'}));
System.out.println(integerEquivalent(new char[] {'*'}));
System.out.println(integerEquivalent(new char[] {}));
}
public static int integerEquivalent (char[] a) {
if (a == null || a.length == 0)
return -1;
int number = 0, digit = 0;
for (int i = 0; i < a.length; i++) {
digit = Character.getNumericValue(a[i]);
//digit = Integer.parseInt(String.valueOf(a[i]));
if (digit == -1) return -1;
else
number = number * 10 + digit;
}
return number;
}
public static String integerEquivalent1 (char[] a) {
if (a == null || a.length == 0)
return "-1";
int digit = 0;
String number = "";
for (int i = 0; i < a.length; i++) {
digit = Character.getNumericValue(a[i]);
//digit = Integer.parseInt(String.valueOf(a[i]));
if (digit == -1) return "-1";
else
number += a[i];
}
return number;
}
}