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.

You May Also Like:

Reverse a string in Java (6 Different Ways)
Splitting a String in Java using a delimiter
Count number of words in a string java
Count total number of times each character appears in the string in java
Check if two strings are anagrams or not in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
Java Program to find the frequency of each character in String ?

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 *