Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions java/StringToInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

import java.util.Scanner;

public class StringToInt {

static int stringtointeger(String str) {
int i = 0;
if (str == null || str.length() < 1) {
return 0;
}

str = str.trim();

char sign = '+';
if (str.charAt(0) == '-') {
sign = '-';
// i++;
} else if (str.charAt(0) == '+'){
// i++;
}

int result = 0;
for( ; i<str.length(); i++){
int curr = str.charAt(i) - '0';
if(curr>=0 && curr<=9) {
if (result == 0){
result = curr;
}
else if(result != 0){
result = result*10 + curr;
}
else if (result > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
else if (result < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
else {
break;
}
}
if(sign == '-')
return - result;
return result;
}

public static void main (String[]args){
System.out.println("Enter a String to convert to Int: ");
Scanner stringchoice = new Scanner(System.in);
String choice = stringchoice.next();

System.out.println("Converted int: " + stringtointeger(choice));
}
}