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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials *Java program to convert string to int without using library functions in java */ public class StringToIntConversionTest { public static void main(String[] args) { try(Scanner scanner = new Scanner(System.in)) { System.out.println("Enter the string you want to convert to int::"); //Taking input from keyboard using Scanner String inputString = scanner.next(); System.out.println("----------------------------------------------"); System.out.println("String Before Conversion in int:" + inputString); int intOutput = convertStringToInt(inputString); System.out.println("String After Conversion in int:" + intOutput); } catch (Exception e) { System.out.println(e.getMessage()); } } public static int convertStringToInt(String inputString) { int i = 0; int number = 0; boolean isNegative = false; int len = inputString.length(); //Check if number is negative then make isNegative flag true if (inputString.charAt(0) == '-') { isNegative = true; i = 1; } // Here you may use for loop as well while (i < len) { number = number*10; number +=(inputString.charAt(i++)-'0'); } //if number is negative again prefix with negative sign if (isNegative){ number = -number; } return number; } } |
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
1 2 3 |
public static int convertStringToInt(String number) { return number.chars().reduce(0, (a, b) -> 10 * a + b - '0'); } |
I have tested above program for four input strings as below but it will work for other input string as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Enter the string you want to convert to int:: 10000 ---------------------------------------------- String Before Conversion in int:10000 String After Conversion in int:10000 Enter the string you want to convert to int:: -892 ---------------------------------------------- String Before Conversion in int:-892 String After Conversion in int:-892 Enter the string you want to convert to int:: 12089 ---------------------------------------------- String Before Conversion in int:12089 String After Conversion in int:12089 Enter the string you want to convert to int:: 0901 ---------------------------------------------- String Before Conversion in int:0901 String After Conversion in int:901 |
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.