How to convert string to int in java without using integer parseInt() method : Code with example

How to convert string to int in java without using integer parseInt() method : Code with example

We want to manually convert string to int without using parseInt() built in method . So let us understand the question first .


As integer can be positive and negative , Here two case arises 

Case 1 :   If string is positive

If user inputs   "12345"   ,then       it should  give output   12345  as an int  number


Case 2 :  If string is negative

If user inputs   "-12345"   ,then      it should  give output   -12345  as an int  number


Read Also :    Count number of times each alphabet appears in the String : Java code with example


Logic :

To convert string to integer , first we need to know what place value each digit must be multiplied by. Working through the digits left to right seems problematic because we don’t know what the place value of the first digit is until we know how long the number is. For example, the first character of "367" is identical to
that of "31"; although it represents 300 in the first case and 30 in the second case.
The most obvious solution is to 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 so on.This enables 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.




string to integer in java program code with example
      Converting String to Integer :  Pseudo Code 

       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




Java Code :


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