-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertStringToInteger.java
35 lines (30 loc) · 1.06 KB
/
ConvertStringToInteger.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
public class ConvertStringToInteger {
//Convert a number from string to integer without using parseInt() method
public static void main(String[] args) {
// TODO Auto-generated method stub
String convertingString="123456";
System.out.println("String Before Conversion : "+ convertingString);
int output= stringToint( convertingString );
System.out.println("");
System.out.println("int value as output "+ output);
System.out.println("");
}
public static int stringToint( String str ){
int i = 0, number = 0;
boolean isNegative = false;
int len = str.length();
if( str.charAt(0) == '-' ){
isNegative = true;
i = 1;
}
while( i < len ){
number *= 10;
System.out.println(number);
number += ( str.charAt(i++) - '0' );
System.out.println(number);
}
if( isNegative )
number = -number;
return number;
}
}