Convert String To Int In Java Without Using Integer ParseInt()

First Approch

To convert string to an integer.

First, we need to know what place value each digit must be multiplied by.

Scan the digits from right to left because the rightmost position is always the one's place, the next to rightmost is always the ten's, and next too.

This allows us to start at the right end of the string with a place value of 1 and work backward through the string, multiplying the place value by 10 each time we move to a new place.

Converting String to Integer: Algorithm

1. Start number at 0 2. If the first character is '-' Set the negative flag Start scanning with the next character For each character in the string Multiply number by 10 Add( digit number - '0' ) to number If negative flag set Negate number Return number

StringtoInt1.java

package com.javacodestuffs.core.strings; public class StringtoInt1 { public static void main (String args[]) { String convertingString = "753211137"; System.out.println("Before converting String to Integer is : "+ convertingString); int convetedInt = convertStringToInt( convertingString ); System.out.println("String converted to Integer "+ convetedInt); } public static int convertStringToInt( 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; number += ( str.charAt(i++) - '0' ); } if( isNegative ) number = -number; return number; } } output: Before converting String to Integer is : 753211137 String converted to Integer 753211137

Second Way

From the given string, create an array of characters for each element, keep the index saved, and multiply its ASCII value by the power of the actual reverse index. Sum the partial factors and you get it.

We need to cast to use Math.pow (since it returns a double), but you can avoid it by creating your own power function.

StringtoInt2.java

package com.javacodestuffs.core.strings; public class StringtoInt2 { public static void main(String args[]) { String convertingString = "753211137"; System.out.println("Before converting String to Integer is : " + convertingString); int convetedInt = convertStringToInt(convertingString); System.out.println("String converted to Integer " + convetedInt); } public static int convertStringToInt(String str) { int res = 0; char[] chars = str.toCharArray(); System.out.println(str.length()); for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) { int temp = chars[j] - 48; int power = (int) Math.pow(10, i); res += temp * power; System.out.println(res); } return res; } } Before converting String to Integer is : 753211137 9 700000000 750000000 753000000 753200000 753210000 753211000 753211100 753211130 753211137 String converted to Integer 753211137

Third Approch - Using Java 8

1). Convert srtNum to char.

2). For each char (represented as 'b') -> 'b' -'0' will give the relative number.

3). Sum all in a (initial value is 0) (each time we perform an opertaion on a char do -> a=a*10 .

StringtoInt3.java

package com.javacodestuffs.core.strings; public class StringtoInt3 { public static void main(String args[]) { String convertingString = "753211137"; System.out.println("Before converting String to Integer is : " + convertingString); int convetedInt = convertStringToInt(convertingString); System.out.println("String converted to Integer " + convetedInt); } public static int convertStringToInt(String strNum) { return strNum.chars().reduce(0, (a, b) - >10 * a + b - '0'); } } output: Before converting String to Integer is : 753211137 String converted to Integer 753211137

In this article, we have seen how to  Convert String To Int In Java Without Using Integer ParseInt() with examples. All source code in the article can be found in the GitHub repository.