How to Convert a String Class to an Integer Class in Java?
Last Updated :
29 Feb, 2024
String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1).
In this article, we will learn to Convert a String to an Integer in Java.
Examples of Converting a String to an Integer
Input: "213456"
Output: 213456
Input: "2134567"
Output: 2134567
To implement this we have two methods as given below.
1. User Implementation
In this method, we will write a user-defined function to convert the given string into an Integer. The idea is to iterate the string from start to end. First, we will declare an Integer variable result to store the Integer value. Then we will simply loop through the string and at each iteration we will multiply the current result by 10 and then add the current character to the result by converting the character to int. We will perform the following tasks to implement this algorithm.
- Declare an Integer variable called result to store the ans.
- Loop from start to end on the given string.
- At each iteration multiply the previous result by 10 so that it will shift by one position on the left.
- Convert the current character of the string to int and add it to the result.
- Return the result.
Below is the Program to Convert a String to an Integer with User Implementation:
Java
public class StringToInteger {
public static Integer convertStringToInteger(String str) {
Integer res = 0;
for (int i = 0; i < str.length(); i++) {
// Multiply the given result by 10 so that it will shift it by one position on left
res = res * 10;
// Convert the current character to Integer and then add it to the previous result
res += (s.charAt(i) - '0');
}
return res;
}
public static void main(String[] args) {
String inp1 = "213456";
System.out.println( convertStringToInteger(inp1));
}
}
Output:
21456
Complexity of the above Program:
Time complexity: O(N)
Space complexity: O(1)
2. Using InBuilt Java function
In this method instead of writing custom logic we will use inbuilt method given by Integer class in java. The method is "Integer.parseInt()". We will give the string to this method and it will give us the resulting int as the ans. Following are the steps to implement the same.
- Declare a Integer variable result.
- Pass the given string to the "Integer.parseInt()" function.
- Store it int the result variable.
- Return the result.
Below is the Program to Convert a String to an Integer with User InBuilt Function:
Java
public class Helper {
public static Integer convertStringToInteger(String s) {
// Parse the string to an integer
Integer result = Integer.parseInt(s);
return result;
}
public static void main(String[] args) {
// Example input string
String inp1 = "213456";
// Convert the input string to an integer
// Using the convertStringToInteger method
Integer output = convertStringToInteger(inp1);
// Print the result
System.out.println(output);
}
}
Output:
21456
Complexity of the above Program:
Time complexity: O(N)
Space complexity: O(1)
Similar Reads
How to Convert a String to a Character in Java? String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin
3 min read
How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210
2 min read
How to Convert Char to String in Java? In this article, we will learn how to Convert Char to String in Java. If we have a char value like 'G' and we want to convert it into an equivalent String like "G" then we can do this by using any of the following three listed methods in Java. Using toString() method of Character classUsing valueOf(
5 min read
How to Convert a String to an Numeric in Java? In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin
2 min read
Java Program to Convert String to Integer Array In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.Example:Below i
3 min read