Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,705 questions

55,464 answers

573 users

How to extract and sort numbers from a string containing numbers and text in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
    This program extracts all integer values from a mixed string
    and sorts them using the standard library's sorting utilities.

    It demonstrates:
      - clear separation of concerns using methods
      - straightforward character parsing
      - dynamic storage using ArrayList<Integer>
      - efficient sorting with Collections.sort
*/
public class ExtractAndSortNumbers {

    // ------------------------------------------------------------
    // Extract all integer values from a mixed string.
    // The method walks through each character, collects digits,
    // and converts completed digit sequences into integers.
    // ------------------------------------------------------------
    public static List<Integer> extractNumbers(String input) {
        List<Integer> numbers = new ArrayList<>();
        StringBuilder buffer = new StringBuilder();

        for (char ch : input.toCharArray()) {
            if (Character.isDigit(ch)) {
                // accumulate digits
                buffer.append(ch);
            } else {
                // flush buffer if it contains a number
                if (buffer.length() > 0) {
                    numbers.add(Integer.parseInt(buffer.toString()));
                    buffer.setLength(0);
                }
            }
        }

        // flush trailing number
        if (buffer.length() > 0) {
            numbers.add(Integer.parseInt(buffer.toString()));
        }

        return numbers;
    }

    // ------------------------------------------------------------
    // Print all numbers in a space‑separated format.
    // ------------------------------------------------------------
    public static void printNumbers(List<Integer> numbers) {
        for (int i = 0; i < numbers.size(); i++) {
            System.out.print(numbers.get(i));
            if (i < numbers.size() - 1) {
                System.out.print(" ");
            }
        }
        System.out.println();
    }

    // ------------------------------------------------------------
    // Main 
    // ------------------------------------------------------------
    public static void main(String[] args) {
        String input = "1000withz7 and3 or 99 give42";

        // extract numbers
        List<Integer> numbers = extractNumbers(input);

        // sort numbers
        Collections.sort(numbers);

        // display result
        System.out.print("Sorted numbers: ");
        printNumbers(numbers);
    }
}



/*
run:

Sorted numbers: 3 7 42 99 1000

*/

 



answered 1 day ago by avibootz
edited 1 day ago by avibootz
...