Open In App

Extract Maximum Numeric Value From A Given Alphanumeric String in Java

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn to extract the maximum numeric value from a given alphanumeric string using Java. We will use regular expressions to identify and isolate numeric sequences within the string, and then compare these values to determine the largest one. This process consists of parsing and comparing integer values extracted from mixed character strings.

Examples:

Input: 100klh564abc365bg Output: 564 //Maximum numeric value among 100, 564 and 365 is 564.

Input: abchsd0365sdhs Output: 365

Java program to Extract Maximum Numeric Value From A Given Alphanumeric String

Extracting the maximum numeric value from a given alphanumeric string uses regular expressions to identify sequences of digits within the string. By applying a regex pattern to match all numeric sequences, the code parses these values, compares them, and determines the highest number found

Illustration:

The following program demonstrates how we can extract maximum value from a given alphanumeric string in Java:

Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MaxNumericValueExtractor {

    /**
     * Extracts the maximum numeric value from the given alphanumeric string.
     * 
     * @param alphanumericString The input alphanumeric string containing numbers
     *                           and letters.
     * @return The maximum numeric value found in the string. Returns 0 if no
     *         numbers are found.
     */
    public static int extractMaxNumericValue(String alphanumericString) {
        // Define a regex pattern to find all sequences of digits
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(alphanumericString);

        // Initialize the maximum value to 0
        int maxValue = 0;

        // Loop through all matches found by the regex pattern
        while (matcher.find()) {
            // Convert the matched numeric sequence to an integer
            int currentNumber = Integer.parseInt(matcher.group());

            // Update maxValue if the current number is greater
            if (currentNumber > maxValue) {
                maxValue = currentNumber;
            }
        }

        return maxValue;
    }

    public static void main(String[] args) {
        // Input string with mixed characters and numbers
        String alphanumericString = "100klh564abc365bg";

        // Extract the maximum numeric value from the string
        int maxValue = extractMaxNumericValue(alphanumericString);

        // Output the result
        System.out.println("The maximum numeric value is: " + maxValue);
    }
}

Output
The maximum numeric value is: 564

Complexity of the above method:

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1), as no extra space is used.

Explanation of the above program:

  • We have imported the Pattern and Matcher classes from the java.util.regex package to use regular expressions for finding numeric sequences.
  • We define a Pattern object with the regex \\d+ to match one or more consecutive digits in the input string.
  • We create a Matcher object to apply the pattern to the alphanumeric string and identify all digit sequences.
  • We iterate through the matches, converting each sequence to an integer and updating the maximum value found.
  • Finally, we return and print the maximum numeric value from the string.

Next Article
Article Tags :
Practice Tags :

Similar Reads