How to convert string to int without using library functions in java

By | July 25, 2018

In this post we will talk and learn How to convert string to int without using library functions in java.

If you want to convert String into number without using a library function then subtract the character ‘0’ from each numeric character to give you its int value, then build up the number by just multiplying the current sum by 10 before adding the next digit’s ìnt value.

The main reason to works Below logic is that the characters 0-9 have consecutive ASCII values, so subtracting ‘0’ from any of them gives you the offset from the character ‘0’ that is of course the numeric equivalent of the character.

Above Logic can we written using JAVA 8 as below:

NOTE:You have to customize this logic for negative as i have done in above main program but off source i will work for all positive numbers

I have tested above program for four input strings as below but it will work for other input string as well.

 

That’s all about how to convert string to int without using library functions in java
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *