Java program to convert String into Integer without Integer.parseInt()

12:12 PM 1 Comments A+ a-

Although Java SDK provides you with convenient methods to convert the compatible data from one data types to another, but sometimes in interviews and other coding competitions you are also supposed to know how to do those conversions without those built-in methods provided by Java SDK itself. In one such case, one of the most common thing in your java code is to convert the string representation of any number back to native int or Integer data type in Java. Although there is a simple method Integer.parseInt(String num) that you can use to get the string num in int format, but in case you need to do that stuff manually, this post will show you how to do that.

Logic building and approach

We will be first reading the string backwards character by character looking for the digit characters only. Till we keep getting the digit characters, we will use those to formulate our number by using the digits and increment the factor by 10 in each step.
We are simply ignoring the non-digit characters here or otherwise in we want, we can convert each character to its number representation, but I don't think that's what we need. And finally, we will also take care of negative numbers as well. For that we will check for the "-" in the very first place in the String input, if it is "-" then we will make the number negative.

Java Program to Manually convert string to int without Integer.parseInt() method

Below is the simple Java method that takes a String str as input and return the int representation of its digits only. 
For example:-
"1263*67%" will return 126367
"a76dsb6l7" will return 76617
"-78fjkd7439" will return -78439
"289289" will return 289289

public int stringToInt(String str) {
  int number = 0, factor = 1; 
  boolean negative = false;
  for (int i = str.length() - 1;i >= 0;i--) {
   //check if the very first character has '-', then the result must be negative
   if((i == 0) && (str.charAt(i) == '-')) {
    negative = true;
    continue;
   }
   //if the string has any non-number characters, continue with the next character ignoring the current one
   else if("0123456789".indexOf( str.charAt(i) ) == -1) {
    continue;
   }
   number+= (str.charAt(i) - '0') * factor;
   factor*= 10;
  }
  if(negative)
   return -number;
  return number;
 }

There might be many other methods to achieve this as well. For instance, we could have used the static methods of Character class such as isDigit() to check for whether the characters are digits or not. In fact, this whole program could have been done using the static methods of Character class. You may want to give it a try!

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!