Java conversion of a group of strings to an array of integer arrays String Array

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class sourc {
public static void main(String[] args) {
        String name = null;
        Integer id = null;
        String strings = "one*10*two*11*three*12";
        StringTokenizer st2 = new StringTokenizer(strings, "*");
        while (st2.hasMoreElements()) {
            name = st2.nextElement().toString();
            id = Integer.parseInt(st2.nextElement().toString());
            String[] str = new String[]{name};
            List<String> al = new ArrayList<String>(Arrays.asList(str));
            System.out.println(al);
            ArrayList<Integer> arrli = new ArrayList<Integer>(id);
            arrli.add(id);
            System.out.println(arrli);
        }

}

I have a conclusion like

[one]

[10]

[two]

[eleven]

[three]

[12]

But I need a conclusion like

[one two Three]

[10,11,12]

+4
source share
4 answers

Why do not you use:

public static void main(String[] args) {
    String strings = "one*10*two*11*three*12";
    String[] spl = strings.split("\\*");//split with *
    ArrayList<Integer> arrli = new ArrayList<>();
    List<String> al = new ArrayList<>();
    for (String s : spl) {//loop throw your resutl
        if (s.matches("\\d+")) {//check if your input is int or not
            arrli.add(Integer.parseInt(s));//if int add it to list of ints
        } else {
            al.add(s);//else add it to list of Strings
        }
    }
    System.out.println(al);//output [10, 11, 12]
    System.out.println(arrli);//output [one, two, three]

}

This will help you if you have two consecutive intsor String, like this:

"one*9*10*two*11*three*four*12"
//--^---^---------^------^

EDIT

Now I have my input like "astv * 12atthh124ggh * dhr1234sfff123 * dgdfg1234 * mnaoj" I need to separate the string and the numbers separately

In this case, you should use templates, for example:

String str = "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
List<String> strings = new ArrayList<>();
List<Integer> nums = new ArrayList<>();
while (m.find()) {
    nums.add(Integer.parseInt(m.group()));
}
p = Pattern.compile("[a-z]+");
m = p.matcher(str);
while (m.find()) {
    strings.add(m.group());
}
System.out.println(nums);
System.out.println(strings);

results

[12, 124, 1234, 123, 1234]
[astv, atthh, ggh, dhr, sfff, dgdfg, mnaoj]
+1
source

, String#split() .

String input = "one*10*two*11*three*12";
String[] words = input.replaceAll("\\d+\\*?", "")
                      .split("\\*");
String[] nums = input.replaceAll("[^0-9*]+\\*?", "")
                     .split("\\*");
+3

You must create two Listoutside the loop and add elements to them inside the loop:

List<String> names = new ArrayList<String>();
List<Integer> ids = new ArrayList<Integer>();
while (st2.hasMoreElements()) {
    names.add(st2.nextElement().toString());
    ids.add(Integer.parseInt(st2.nextElement().toString()));
}
System.out.println(names);
System.out.println(ids);

Note that this code makes assumptions on the input (even numbers of elements, where the second element of each pair is an integer) and will fail if the input does not meet these assumptions.

+2
source

Why not use split?

public static void main(final String[] args) {
    final String strings = "one*10*two*11*three*12";


    final String[] split = strings.split("\\*");

    final List<String> resultStrings = new ArrayList<>();
    final List<String> resultInt = new ArrayList<>();

    for (int i = 0; i < split.length; i++) {
        if (i % 2 == 0) {
            resultInt.add(split[i]);
        } else {
            resultStrings.add(split[i]);
        }
    }

    System.out.println(resultInt);
    System.out.println(resultStrings);
}
+1
source

Source: https://habr.com/ru/post/1676805/


All Articles