-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtoI.java
54 lines (43 loc) · 844 Bytes
/
AtoI.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
package com.sai;
import java.util.Scanner;
class AtoI {
long num = 0;
int l = 0;
boolean bool = true;
int atoi(String str1) {
if (str1 == null)
return 0;
String str = str1.trim();
int len = str.length();
if (len == 0) {
return 0;
}
char[] c = str.toCharArray();
if (c[l] == '-') {
bool = false;
l++;
} else if (c[l] == '+') {
l++;
}
for (; l < str.length(); l++) {
if (c[l] < '0' || c[l] > '9') {
return -1;
}
int m = c[l] - '0';
num = num * 10 + m;
}
if (bool) {
return (int) num;
} else {
return (int) (-1 * num);
}
}
public static void main(String[] args) {
AtoI a = new AtoI();
System.out.println("Enter a string:");
Scanner scan = new Scanner(System.in);
int x = a.atoi(scan.nextLine());
System.out.println("Result is " + x);
scan.close();
}
}