Converting String to int without any library function

public static int stringToInt(String number) {
    int res = 0;
    for (int i = 0; i < number.length(); i++) {
        res = res * 10 + number.charAt(i) - '0';
    }
    return res;
}

Perform whatever calculation you want to perform and then use the following method to convert the int back to String without any library function

Converting int to String without any library function

   public static String parseInt(int integer)
{
    boolean ifNegative = integer<0;
    boolean ifMin = integer == Integer.MIN_VALUE;
    StringBuilder builder = new StringBuilder();        
    integer = ifNegative?(ifMin?Integer.MAX_VALUE:-integer):integer;    
    List<Integer> list = new LinkedList<Integer>(); 
    int remaining = integer;
    int currentDigit = 0 ;

    while(true)
    {
        currentDigit = remaining%10;
        list.add(currentDigit);
        remaining /= 10;
        if(remaining==0) break;
    }

    currentDigit = list.remove(0);
    builder.append(ifMin?currentDigit+1:currentDigit);
    for(int c : list)
        builder.append(c);
    builder.reverse().insert(0, ifNegative?'-':'+');
    return builder.toString();
}
Answer from Yousaf on Stack Overflow
🌐
Blogger
javahungry.blogspot.com › 2014 › 02 › how-to-convert-string-to-int-in-java-without-using-integer-parseint-method-code-with-example.html
How to convert string to int in java without using integer parseInt() method : Code with example | Java Hungry
public class StringtoInt { public static void main (String args[]) { String convertingString="123456"; System.out.println("String Before Conversion : "+ convertingString); int output= stringToint( convertingString ); System.out.println(""); 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; number += ( str.charAt(i++) - '0' ); } if( isNegative ) number = -number; return number; } } Please write in comments in case if you have any doubts
Top answer
1 of 2
1

Converting String to int without any library function

public static int stringToInt(String number) {
    int res = 0;
    for (int i = 0; i < number.length(); i++) {
        res = res * 10 + number.charAt(i) - '0';
    }
    return res;
}

Perform whatever calculation you want to perform and then use the following method to convert the int back to String without any library function

Converting int to String without any library function

   public static String parseInt(int integer)
{
    boolean ifNegative = integer<0;
    boolean ifMin = integer == Integer.MIN_VALUE;
    StringBuilder builder = new StringBuilder();        
    integer = ifNegative?(ifMin?Integer.MAX_VALUE:-integer):integer;    
    List<Integer> list = new LinkedList<Integer>(); 
    int remaining = integer;
    int currentDigit = 0 ;

    while(true)
    {
        currentDigit = remaining%10;
        list.add(currentDigit);
        remaining /= 10;
        if(remaining==0) break;
    }

    currentDigit = list.remove(0);
    builder.append(ifMin?currentDigit+1:currentDigit);
    for(int c : list)
        builder.append(c);
    builder.reverse().insert(0, ifNegative?'-':'+');
    return builder.toString();
}
2 of 2
1

The source code for Integer.parseInt is available on GrepCode. It uses a package-private method to generate NumberFormatException errors, but you can leave those out and the code will still work for valid strings.

public static int parseInt(String s, int radix) {
    int result = 0;
    boolean negative = false;
    int i = 0, len = s.length();
    int limit = -Integer.MAX_VALUE;
    int multmin;
    int digit;

    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar < '0') {
            if (firstChar == '-') {
                negative = true;
                limit = Integer.MIN_VALUE;
            }
            i++;
        }
        multmin = limit / radix;
        while (i < len) {
            digit = Character.digit(s.charAt(i++), radix);
            result *= radix;
            result -= digit;
        }
    }
    return negative ? result : -result;
}
🌐
Baeldung
baeldung.com › home › java › java string › convert string to int or integer in java
Convert String to int or Integer in Java | Baeldung
December 15, 2023 - Therefore, it’s highly recommended to use valueOf() instead of parseInt() to extract boxed integers as it may lead to a better overall footprint for our application. ... @Test public void givenString_whenCallingIntegerConstructor_shouldCo...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-string-to-integer-without-using-any-in-built-functions
Convert string to integer without using any in-built functions - GeeksforGeeks
Approach: The idea is to use the ASCII value of the digits from 0 to 9 start from 48 - 57. Therefore, to change the numeric character to an integer subtract 48 from the ASCII value of the character will give the corresponding digit for the given ...
Published: July 15, 2025
🌐
Quora
quora.com › Can-a-string-be-converted-to-an-integer-without-using-parseInt-or-Integer-parseInt-methods-in-Java
Can a string be converted to an integer without using parseInt() or Integer.parseInt() methods in Java? - Quora
Answer (1 of 2): Yes , You can do with valueOf() method. Example 1: String str=”100″; Integer num=new Integer (8); num=Integer.valueOf(str); Example 2: Below program illustrates the java.lang.Integer.valueOf(String str, int base) method: // Base 2 Integer value=Integer.valueOf(“1010”,8); ...
🌐
Glarity
askai.glarity.app › search › Java-code-to-convert-string-to-int-without-using--parseInt
Java code to convert string to int without using `parseInt()`. - Ask and Answer - Glarity
- For each character, convert it into its corresponding integer value (by subtracting the ASCII value of '0'). - Multiply the current result by 10 (shifting the decimal place) and add the current digit.
🌐
IONOS
ionos.com › digital guide › websites › web development › java string to int
How to convert a string to int in Java - IONOS
January 2, 2025 - If you have a string that only contains whole numbers, you can convert it into the Java primitive int, which stands for integer. While there are 5 different ways to convert a Java int to a string, there are only 2 main ways to convert a Java string to an int. They are Integer parseInt() and Integer.valueOf().
🌐
javathinking
javathinking.com › blog › convert-string-to-integer-without-using-parse-java
Converting String to Integer without Using `parseInt` in Java — javathinking.com
In Java, the `Integer.parseInt()` method is a common and straightforward way to convert a string representation of a number into an integer. However, there are situations where you might want to avoid using this built-in method, such as in technical interviews or when you want to have more control over the conversion process.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › java › how do i convert a string to an int in java?
How do I convert a String to an int in Java? | Sentry
class Main { public static void main(String[] args) { String validString = "123"; String invalidString = "123x"; int number; try { number = Integer.parseInt(validString); System.out.println("Converted integer: " + number); number = Integer.parseInt(invalidString); System.out.println("Converted integer: " + number); } catch (NumberFormatException e) { System.out.println("Invalid integer input"); } } } Modern Java versions running on modern systems are very efficient and there should be practically no performance difference between using primitive int and declaring new Integer objects, so you should consider convenience over performance in nearly all cases.
🌐
Facebook
facebook.com › groups › codewithfun › posts › 1100729830017461
How To Convert String To Int In Java Without Using Integer ...
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
Top answer
1 of 3
12

To improve your intToString() method you should consider using a StringBuilder, and specifically the method StringBuilder.append(int).

Iterate digits in your int, and for each digit you can append(eachDigit) to the StringBuilder element. This will also reduce the complexity of intToString() to \$O(n)\$ since you do not need to create a new String instance each iteration. To get a String object from the StringBuilder, use StringBuilder.toString(). Or, if you are not allowed, you can use StringBuilder.subString(0).

You should also use a StringBuilder.append() (using the same idea) to reverse the resulting string (your second loop in your code).

Since it is not homework (as per comments), I have no problems providing a code snap. It should look something like this:

public static String intToString(int n) { 
    if (n == 0) return "0";
    StringBuilder sb = new StringBuilder();
    while (n > 0) { 
        int curr = n % 10;
        n = n/10;
        sb.append(curr);
    }
    String s = sb.substring(0);
    sb = new StringBuilder();
    for (int i = s.length() -1; i >= 0; i--) { 
        sb.append(s.charAt(i));
    }
    return sb.substring(0);
}

Notes:

  • You can also use StringBuilder.reverse() instead of the second loop.
  • In here, \$O(n)\$ means linear in the the number of digits in the input number (n is the number of digits in the input number - not the number itself!) If you are looking for the complexity in terms of the initial number (it is \$O(\log(n))\$) since you divide your element by 10 each iterations, you have a total of \$\log_{10}(\text{number})\$ iterations for each loop, which results in \$O(\log(\text{number}))\$.
2 of 3
2

For your example , I should point out some problems : first , when you add two add string frequently, you should use StringBuilder instead ; second , you should consider Integer.MIN_VALUE into account!Here is my code:

public static String parseInt(int integer)
{
    boolean ifNegative = integer<0;
    boolean ifMin = integer == Integer.MIN_VALUE;
    StringBuilder builder = new StringBuilder();        
    integer = ifNegative?(ifMin?Integer.MAX_VALUE:-integer):integer;    
    List<Integer> list = new LinkedList<Integer>(); 
    int remaining = integer;
    int currentDigit = 0 ;

    while(true)
    {
        currentDigit = remaining%10;
        list.add(currentDigit);
        remaining /= 10;
        if(remaining==0) break;
    }

    currentDigit = list.remove(0);
    builder.append(ifMin?currentDigit+1:currentDigit);
    for(int c : list)
        builder.append(c);
    builder.reverse().insert(0, ifNegative?'-':'+');
    return builder.toString();
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-string-to-int-in-java
String to int in Java - GeeksforGeeks
Converting a String to an int in Java can be done using methods provided in the Integer class, such as Integer.parseInt() or Integer.valueOf() methods. Example: The most common method to convert a string to a primitive int is Integer.parseInt(). ...
Published: July 23, 2025
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-string-to-int-in-java
Java String to int Conversion
June 10, 2024 - String number = "123"; int result = Integer.parseInt(number); System.out.println(result); // Output: 123 · The Integer.valueOf() method converts a String to an Integer object, which can then be unboxed to a primitive int.
🌐
Tpoint Tech
tpointtech.com › java-string-to-int
Java Convert String to int
March 17, 2025 - In Java, the conversion of a String variable into an int variable is considered a common process or operation.
🌐
Devmio
devm.io › java › convert-java-string-int-134101
How to convert a Java String to an Int
May 17, 2017 - In this tutorial, Allice Watson, explains how a String can be converted into an int data type in Java [examples included].
🌐
freeCodeCamp
freecodecamp.org › news › java-string-to-int-how-to-convert-a-string-to-an-integer
Java String to Int – How to Convert a String to an Integer
November 23, 2020 - This leads us to the question – how can we convert a string to an integer? In Java, we can use Integer.valueOf() and Integer.parseInt() to convert a string to an integer.
🌐
Java67
java67.com › 2013 › 03 › how-to-convert-string-to-int-in-java.html
How to convert String to int or Integer data type in Java? Example Tutorial | Java67
There are 3 main ways to convert String to int in Java, first, by using the constructor of Integer class, second, by using parseInt() method of java.lang.Integer, and third, by using Integer.valueOf() method.