Java Program to Convert String to Integer by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Find Frequency of Consonants in String by Using Recursion

In this program we are going to see how to convert string to integer using Recursion by Java programming language. Also see these while reading.

  • String to integer coding ninjas
  • Recursion convert string to integer coding blocks
  • Replace character in string java using recursion
  • Pair star using recursion in java
  • String to integer in java

Java Program to Convert String to Integer by Using Recursion

Assume a string “12345”

Then the converted integer = 12345

Now let’s see different ways to convert string to integer by using Recursion.

Method-1: Java Program to Convert String to Integer by Using Recursion by Using Static Input and Recursion

Approach:

  • Declare and initialize a String variable say ‘str’.
  • Call a user defined method convertToInt() method and pass the string ‘str’ as parameter.
  • Inside the user defined method we will check if the string contains only a single digit number then return its value to the main() method.
  • Else store the 1st char of the string in a double variable say ‘a’ then recursively call the sub-string starting at the second character and store in a double variable say ‘b’.
  • Now, we will convert the string value into an integer value using the formula “(a * Math.pow(10, str.length() – 1) + b)” and store the value in a double variable say ‘c’ and then return the value of  ‘c’ to the main() method
  • Now the value of the user defined method convertToInt() method is stored in an integer variable say ‘n’ inside the main() method.
  • Print the value of converted integer.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // declaring and initiating a string variable 'str'
        String str = "98765";
        //calling convertToInt() method and storing the value inside an integer variable 'n'
        int n = convertToInt(str);
        // print the converted integer value
        System.out.print("The converted integer value is: "+n);
    }
    
    //convertToInt() method to convert string to integer
    static int convertToInt(String str)
    {
        // If the string contains only a single digit number
        if (str.length() == 1)
            return (str.charAt(0) - '0');
        // First digit of the number
        double a = str.charAt(0) - '0';
        // Recursive call for the sub-string starting at the second character
        double b = convertToInt(str.substring(1));
        // First digit multiplied by the appropriate power of 10 and then add the recursive result
        double c = a * Math.pow(10, str.length() - 1) + b;
        return (int)(c);	
    }
}
Output:

The converted integer value is: 98765

Method-2: Java Program to Convert String to Integer by Using Recursion By Using User Input and Recursion

Approach:

  • Declare a String variable say ‘str’.
  • Prompt the user to enter the values for the string.
  • Call a user defined method convertToInt() method and pass the string ‘str’ as parameter.
  • Inside the user defined method we will check if the string contains only a single digit number then return its value to the main() method.
  • Else store the 1st char of the string in a double variable say ‘a’ then recursively call the sub-string starting at the second character and store in a double variable say ‘b’.
  • Now, we will convert the string value into an integer value using the formula “(a * Math.pow(10, str.length() – 1) + b)” and store the value in a double variable say ‘c’ and then return the value of  ‘c’ to the main() method
  • Now the value of the user defined method convertToInt() method is stored in an integer variable say ‘n’ inside the main() method.
  • Print the value of converted integer.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a string value: ");
        //declare an integer variable ‘str’and initialize it by user input using scanner class.
        String str = s.nextLine();
        //calling convertToInt() method and storing the value inside an integer variable 'n'
        int n = convertToInt(str);
        // print the converted integer value
        System.out.print("The converted integer value is: "+n);
    }
    
    //convertToInt() method to convert string to integer
    static int convertToInt(String str)
    {
        // If the string contains only a single digit number
        if (str.length() == 1)
            return (str.charAt(0) - '0');
        // First digit of the number
        double a = str.charAt(0) - '0';
        // Recursive call for the sub-string starting at the second character
        double b = convertToInt(str.substring(1));
        // First digit multiplied by the appropriate power of 10 and then add the recursive result
        a = a * Math.pow(10, str.length() - 1) + b;
        return (int)(a);	
    }
}
Output:

Enter a string value: 
1234
The converted integer value is: 1234

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Also Read:

  1. Convert int to string without using library functions in java?
  2. Write a c program to convert a string of number to integer?
  3. Write a python program to converting an integer to a string in any base?
  4. Java program to convert string to integer by using recursion?

Related Java Programs: