# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.58141184,"math_prob":0.95558566,"size":5295,"snap":"2022-40-2023-06","text_gpt3_token_len":1209,"char_repetition_ratio":0.17879418,"word_repetition_ratio":0.63914025,"special_character_ratio":0.25155807,"punctuation_ratio":0.10526316,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9955253,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-01T05:03:37Z\",\"WARC-Record-ID\":\"<urn:uuid:24d92413-e4f4-420b-87fe-5d2f09ab65ad>\",\"Content-Length\":\"53393\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c2510e59-355b-4f9d-b308-46bde1f26082>\",\"WARC-Concurrent-To\":\"<urn:uuid:1729a287-4a5e-41d1-9c6f-edffa177a4ff>\",\"WARC-IP-Address\":\"159.89.170.46\",\"WARC-Target-URI\":\"https://btechgeeks.com/java-program-to-convert-string-to-integer-by-using-recursion/\",\"WARC-Payload-Digest\":\"sha1:Q34LRAMRO3WZZNRQNOXMKJZEEVOY7HR3\",\"WARC-Block-Digest\":\"sha1:M7JD6YEXGOVQRY7H2L2AG6VNM4IEKBEL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499911.86_warc_CC-MAIN-20230201045500-20230201075500-00346.warc.gz\"}"}