Java Integer parseInt() Method

The Integer.parseInt() method in Java is used to convert a String into an int primitive.

Table of Contents

  1. Introduction
  2. parseInt() Method Syntax
  3. Examples
    • Converting a Decimal String to an int
    • Converting a String with a Radix
    • Handling NumberFormatException
  4. Real-World Use Case
  5. Conclusion

Introduction

The Integer.parseInt() method is a static method in the Integer class in Java. It converts a string representation of a number to an int primitive. This method is useful when you need to convert numeric strings into integers for calculations or other operations.

parseInt()() Method Syntax

The Integer.parseInt() method has two overloaded versions:

1. Basic parseInt Method

public static int parseInt(String s) throws NumberFormatException
  • s: The string to be parsed.
  • The method returns an int primitive representing the value of the specified string.
  • Throws NumberFormatException if the string does not contain a parsable integer.

2. parseInt Method with Radix

public static int parseInt(String s, int radix) throws NumberFormatException
  • s: The string to be parsed.
  • radix: The radix to be used while parsing (for example, 10 for decimal, 16 for hexadecimal).
  • The method returns an int primitive representing the value of the specified string in the specified radix.
  • Throws NumberFormatException if the string does not contain a parsable integer.

Examples

Converting a Decimal String to an int

The parseInt() method can be used to convert a decimal string to an int.

Example

public class ParseIntExample {
    public static void main(String[] args) {
        String numberString = "123";
        int number = Integer.parseInt(numberString);

        System.out.println("Parsed integer: " + number);
    }
}

Output:

Parsed integer: 123

In this example, the method converts the string "123" into the integer 123.

Converting a String with a Radix

The parseInt() method can also be used to convert a string in a different numeral system (radix) to an int.

Example

public class ParseIntWithRadixExample {
    public static void main(String[] args) {
        String hexString = "7B";
        int number = Integer.parseInt(hexString, 16);

        System.out.println("Parsed integer (hex): " + number);
    }
}

Output:

Parsed integer (hex): 123

In this example, the method converts the hexadecimal string "7B" into the integer 123.

Handling NumberFormatException

If the input string is not a valid representation of an integer, the parseInt() method throws a NumberFormatException.

Example

public class ParseIntInvalidExample {
    public static void main(String[] args) {
        String invalidString = "abc";

        try {
            int number = Integer.parseInt(invalidString);
            System.out.println("Parsed integer: " + number);
        } catch (NumberFormatException e) {
            System.out.println("Invalid string for parsing: " + invalidString);
        }
    }
}

Output:

Invalid string for parsing: abc

In this example, the method throws a NumberFormatException because the string "abc" is not a valid representation of an integer.

Real-World Use Case

Converting User Input

In a real-world application, you might need to convert user input, which is often in the form of strings, to integers for calculations.

Example

import java.util.Scanner;

public class UserInputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter an integer: ");

        String input = scanner.nextLine();

        try {
            int number = Integer.parseInt(input);
            System.out.println("You entered: " + number);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input: " + input);
        }

        scanner.close();
    }
}

Output (example input "123"):

Enter an integer:
You entered: 123

Output (example input "abc"):

Enter an integer:
Invalid input: abc

In this example, the user input is read as a string and then converted to an integer. If the input is not a valid integer, an error message is displayed.

Conclusion

The Integer.parseInt() method in Java is a powerful and useful tool for converting strings to integers. By understanding how to use this method, you can efficiently handle tasks that involve parsing numeric strings in your Java applications. Whether you are dealing with decimal strings, strings in different numeral systems, or handling invalid inputs, the parseInt() method provides a reliable solution for these tasks.

Comments