Converting a String to an Integer is a common operation in Java, especially when dealing with user input or reading data from files. In this blog post, we will explore various methods to convert a String to an Integer and provide practical examples with outputs to illustrate each approach.
Method 1: Using Integer.parseInt() Method
The Integer.parseInt() method is a straightforward way to convert a String to an Integer. It parses the string argument as a signed decimal integer and returns the resulting numeric value.
Example:
public class StringToIntegerExample {
public static void main(String[] args) {
String strNumber = "42";
try {
// Convert String to Integer using parseInt()
int intValue = Integer.parseInt(strNumber);
System.out.println("Input String: " + strNumber);
System.out.println("Integer Value: " + intValue);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input for integer conversion");
}
}
}
Output:
Input String: 42
Integer Value: 42
Method 2: Using Integer.valueOf() Method
The Integer.valueOf() method converts a String to an Integer object. It returns an Integer instance representing the specified int value if the String is a valid representation of an integer.
Example:
public class StringToIntegerExample {
public static void main(String[] args) {
String strNumber = "100";
try {
// Convert String to Integer using valueOf()
Integer integerValue = Integer.valueOf(strNumber);
System.out.println("Input String: " + strNumber);
System.out.println("Integer Value: " + integerValue);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input for integer conversion");
}
}
}
Output:
Input String: 100
Integer Value: 100
Method 3: Using NumberFormat.parse() Method
You can also use NumberFormat to parse the String and convert it to an Integer.
Example:
import java.text.NumberFormat;
import java.text.ParseException;
public class StringToIntegerExample {
public static void main(String[] args) {
String strNumber = "2022";
// Convert String to Integer using NumberFormat
try {
NumberFormat numberFormat = NumberFormat.getInstance();
int intValue = numberFormat.parse(strNumber).intValue();
System.out.println("Input String: " + strNumber);
System.out.println("Integer Value: " + intValue);
} catch (ParseException e) {
System.out.println("Error: Invalid input for integer conversion");
}
}
}
Output:
Input String: 2022
Integer Value: 2022
Method 4: Using Scanner class
The Scanner class can also be used to convert a String to an Integer.
Example:
import java.util.Scanner;
public class StringToIntegerExample {
public static void main(String[] args) {
String strNumber = "999";
// Convert String to Integer using Scanner
try {
Scanner scanner = new Scanner(strNumber);
int intValue = scanner.nextInt();
System.out.println("Input String: " + strNumber);
System.out.println("Integer Value: " + intValue);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input for integer conversion");
}
}
}
Output:
Input String: 999
Integer Value: 999
Conclusion
Converting a String to an Integer is a fundamental operation in Java. You can use methods like Integer.parseInt(), Integer.valueOf(), NumberFormat or Scanner to achieve this. In this blog post, we provided practical examples with outputs to demonstrate each method's effectiveness. Remember to handle exceptions appropriately to gracefully handle invalid input and ensure the reliability of your code.
Related String Conversion Examples
- Java Convert Integer to String Example
- Java Convert Float to String Example
- Java Convert Double to String Example
- Java Convert Short to String Example
- Java Convert Long to String Example
- Java Convert Character to String Example
- Java Convert Byte to String Example
- Java Convert Boolean to String Example
- Java Convert String To Short Example
- Java Convert String To Long Example
- Java Convert String To Integer Example
- Java Convert String To Float Example
- Java Convert String To Double Example
- Java Convert String To Byte Example
- Java Convert String To Boolean Example
Comments
Post a Comment
Leave Comment