How to extract multiple integers from String in Java?

I got a series of lines like "(123, 234; 345, 456) (567, 788; 899, 900)" . "(123, 234; 345, 456) (567, 788; 899, 900)" How to extract these numbers into an array like aArray[0]=123, aArray=[234], ....aArray[8]=900;

thanks

-1
source share
11 answers

This is probably too confusing, but the hay ...

The first thing we need to do is remove all the crap we don't need ...

 String[] crap = {"(", ")", ",", ";"}; String text = "(123, 234; 345, 456) (567, 788; 899, 900)"; for (String replace : crap) { text = text.replace(replace, " ").trim(); } // This replaces any multiple spaces with a single space while (text.contains(" ")) { text = text.replace(" ", " "); } 

Next, we need to separate the individual elements of the string into a more manageable form.

 String[] values = text.split(" "); 

Then we need to convert each String value to an int value

 int[] iValues = new int[values.length]; for (int index = 0; index < values.length; index++) { String sValue = values[index]; iValues[index] = Integer.parseInt(values[index].trim()); } 

Then we print the values ​​...

 for (int value : iValues) { System.out.println(value); } 
+5
source

Strategy: Find one or more numbers that are together, through a regular expression to be added to the list.

Code:

  LinkedList<String> list = new LinkedList<>(); Matcher matcher = Pattern.compile("\\d+").matcher("(123, 234; 345, 456) (567, 788; 899, 900)"); while (matcher.find()) { list.add(matcher.group()); } String[] array = list.toArray(new String[list.size()]); System.out.println(Arrays.toString(array)); 

Output:

 [123, 234, 345, 456, 567, 788, 899, 900] 
+4
source

You almost certainly saw a quote:

Some people, faced with a problem, think: "I know, I will use regular expressions." Now they have two problems.

But regular expressions really are your friend for these kinds of things.

 import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; class Numbers { public static void main(String[] args) { String s = "(123, 234; 345, 456) (567, 788; 899, 900)"; Matcher m = Pattern.compile("\\d+").matcher(s); List<Integer> numbers = new ArrayList<Integer>(); while(m.find()) { numbers.add(Integer.parseInt(m.group())); } System.out.println(numbers); } } 

Outputs:

 [123, 234, 345, 456, 567, 788, 899, 900] 
+4
source

Swipe through each character and store the numbers in a temporary array until you find a character (e.g.,,, ; ), then save the data from the temporary array to your array and then delete this temporary array for next use.

+2
source

Since your numbers are separated by a specific character set, you can take a look at the .split(String regex) method.

0
source

Since there may be several different delimiters in your string, you can go through it and replace all the obscure characters with spaces . Then you can use split("\\s") to split your string into an array of substrings with numbers. And finally, convert them to numbers.

0
source

This method will extract integers from the given string. It also processes strings in which other characters are used to separate numbers, and not just those specified in your example:

 public static Integer[] extractIntegers( final String source ) { final int length = source.length(); final char[] chars = source.toCharArray(); final List< Integer > list = new ArrayList< Integer >(); for ( int i = 0; i < length; i++ ) { // Find the start of an integer: it must be a digit or a sign character if ( chars[ i ] == '-' || chars[ i ] == '+' || Character.isDigit( chars[ i ] ) ) { final int start = i; // Find the end of the integer: for ( i++; i < length && Character.isDigit( chars[ i ] ); i++ ) ; // Now extract this integer: list.add( Integer.valueOf( source.substring( start, i ) ) ); } } return list.toArray( new Integer[ list.size() ] ); } 

Note: Since the inner positions of the for loop are after the integer, and the outer for loop increments the variable i when searching for the next integer, the algorithm will require at least one character to separate the integers, but I think this is desirable. For example, the source "-23-12" will produce the numbers [ -23, 12 ] and NOT [ -23, -12 ] (but the "-23 -12" will generate [-23, -12] as expected).

0
source

The easiest way is to use a combination of String.indexOf() (or something like this) and NumberFormat.parse(ParsePosition) . The algorithm will follow:

  • start at the beginning of the line
  • find a number starting at this position
  • analyze the NumberFormat method used, which will stop at a non-digital character and return a value
  • repeat 2), starting from this position (to the end of the line)

At the same time, the string has a certain structure, so IMHO some parser will be better suited, since it will also check the correctness of the format (if necessary, I don’t know). There are many tools for generating Java code from a grammar description (e.g. ANTLR, etc.). But this may be too complicated a solution for the occasion.

0
source

I think you can use regex to get the result. Something like this is possible:

 String string = "(123, 234; 345, 456) (567, 788; 899, 900)"; String[] split = string.split("[^\\d]+"); int number; ArrayList<Integer> numberList = new ArrayList<Integer>(); for(int index = 0; index < split.length; index++){ try{ number = Integer.parseInt(split[index]); numberList.add(number); }catch(Exception exe){ } } Integer[] numberArray = numberList.toArray(new Integer[numberList.size()]); for(int index = 0; index < numberArray.length; index++){ System.out.println(numberArray[index]); } 
0
source

Another way. Maybe it’s good if you want to write less code, it can be bad if you can’t add libraries to your project.

 import com.google.common.base.Splitter; import com.google.common.collect.Iterables; public static void main(String[] args) throws IOException { String text = "(123, 234; 345, 456) (567, 788; 899, 900)"; Splitter splitter = Splitter.onPattern("[,;\\)\\(]").omitEmptyStrings(); String[] cleanString = Iterables.toArray(splitter.split(text), String.class); System.out.println(Arrays.toString(cleanString)); } 

AM sure gurus can cleanse it further.

0
source
  for (int i = 0; i < faces.total(); i++) { CvRect r = new CvRect(cvGetSeqElem("(123, 234; 345, 456)", i)); String x=""+Integer.toString(rx()); String y=""+Integer.toString(ry()); String w=""+Integer.toString(r.width()); String h=""+Integer.toString(r.height()); for(int j=0;j<(4-Integer.toString(rx()).length());j++) x="0"+x; for(int j=0;j<(4-Integer.toString(ry()).length());j++) y="0"+y; for(int j=0;j<(4-Integer.toString(r.width()).length());j++) w="0"+w; for(int j=0;j<(4-Integer.toString(r.height()).length());j++) h="0"+h; r_return=""+x+y+w+h; } 

Above the code will return the string "0123023403540456"

 int[] rectArray = new int[rectInfo.length()/4]; for(int i=0;i<rectInfo.length()/4; i++) { rectArray[i]=Integer.valueOf(rectInfo.substring(i*4, i*4+4)); } 

and he will receive [123, 234, 345, 456]

0
source

All Articles