Java extracting numbers from strings

In Java, if you enter a string of numbers separated by other non numeric characters, but want to extract the numbers, there are many solutions on the Internet. Here is a brief summary of these methods and their corresponding situations, and put forward your own solutions:

1. Multiple single numbers separated by indeterminate characters

This method is only suitable for separating single digit character in string, so the result is also one digit number.
For example:

1 5,2,5. 1    9 2

At this time, you can consider storing the input into the String variable, reading each character in the String in turn, judging whether it is a number and then converting it into integer data. However, there is a very common and simple conversion method on the Internet to convert a single char type character into an integer type character, that is, to subtract the target number character from the '0' character, as shown below:

import java.util.ArrayList;
import java.util.Scanner;

public class NumberCollector {
    public static void main(String[] arge) {
        Scanner in = new Scanner(System.in);
        String charList = in.nextLine();
        ArrayList<Integer> numList = new ArrayList<>();

        //Traverses each character in a string, converting numeric characters to integer numbers
        for (int i = 0; i < charList.length(); i++) {
            if (Character.isDigit(charList.charAt(i))) {
                //By subtracting the target number character from the '0' character ASCII code, the result is the integer number of the number itself
                numList.add(charList.charAt(i) - '0');
            }
        }

        for (int i = 0; i < numList.size(); i++) {
            System.out.println(numList.get(i));
        }
    }
}

2. Multiple arbitrary numbers separated by specific characters

Compared with the first method, this method can filter out non single digit numbers, but before operation, it needs to clearly segment the characters of the numbers.
In the input string, numbers are separated from numbers by unique characters, such as:

5 4 6 12 100 3
1,2,51,12,6

At this time, you can use a general processing method given on the Internet, split the string, and then use the ParseInt method in Integer to convert it:

import java.util.ArrayList;
import java.util.Scanner;

public class NumberCollector {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] numStr = in.nextLine().split(" "); //Take space character segmentation as an example
        ArrayList<Integer> numList = new ArrayList<>();

        //Traversing the segmented string array and transforming it into integer in turn
        for (int i = 0; i < numStr.length; i++) {
            numList.add(Integer.parseInt(numStr[i]));
        }

        for (int i = 0; i < numList.size(); i++) {
            System.out.println(numList.get(i));
        }
    }
}

3. Any number separated by any character

In the development process, I often encounter strange and tricky situations. Here I put forward some personal ideas and solutions, such as questions. If I encounter such situations, for example, input:

5   10,,,103xyz50 a.10

Solution: if you want to extract the numbers and convert them into integer numbers, you can consider traversing each character in the string. If the character is a number, you can store the number character in the preparation area. When you read non numeric characters, you can think that there is a complete number string in the preparation area, and then consider converting it into integer numbers.
The implementation code is as follows:

import java.util.ArrayList;
import java.util.Scanner;

public class NumberCollector {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String charList = in.nextLine();
        in.close();

        String integer = ""; //As a prelude to a numeric string
        ArrayList<Integer> numList = new ArrayList<>();

        for (int i = 0; i < charList.length(); i++) {
            //When traversing each character in the string, if the character is a numeric character, it is stored in the preparation area
            if (Character.isDigit(charList.charAt(i))) {
                integer += charList.charAt(i);

                //Avoid missing collection when the last bit of the string is a number
                if (i == charList.length() - 1) {
                    numList.add(Integer.parseInt(integer));
                }
            }else if (!integer.isEmpty()) { //Read non numeric characters and check if the reserve is empty
                numList.add(Integer.parseInt(integer));
                integer = "";
            }
        }

        //Output collected integer numbers
        for (int each : numList) {
            System.out.println(each);
        }
    }
}

For the input example given above, you can get the output:

5
10
103
50
10

This method can also be applied to the first and second methods, but if the problem is very simple, of course, the first and second methods are time-saving and labor-saving.

The above are my own methods and skills summarized on the Internet and on my own. If there is any omission or better suggestions, I'm very welcome to comment or put forward suggestions, which will also help me a lot!

Tags: Java ascii

Posted on Fri, 15 May 2020 00:41:37 -0400 by anthrt