4
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 output like

[one]

[10]

[two]

[11]

[three]

[12]

But i need output like

[one,two,three]

[10,11,12]

0

4 Answers 4

3

You don't need a tokenizer here, rather you can just split the string using String#split() with some regex replacement logic.

String input = "one*10*two*11*three*12";
String[] words = input.replaceAll("\\d+\\*?", "")
                      .split("\\*");
String[] nums = input.replaceAll("[^0-9*]+\\*?", "")
                     .split("\\*");
3
  • I like the answer Commented May 10, 2017 at 7:52
  • 1
    @AndyTurner Get with the program, Java is going functional. If your entire program takes up more than 3 lines then it's worthless :-) Commented May 10, 2017 at 7:54
  • Frankly, I would really dislike reading something like this in production code, just for the purpose of splitting a string by a delimiter. Commented May 10, 2017 at 8:10
2

You should create two Lists outside the loop, and add the 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 doesn't match these assumptions.

1
  • hi Eran now i have a situation my input like "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj" what i want to update Commented May 10, 2017 at 9:51
1

Why you don't 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 in case you have two successive ints or String like this :

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

EDIT

now am having my input like "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj" i need to split string and numeric separately

In this case you have to use Patterns 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);

Outputs

[12, 124, 1234, 123, 1234]
[astv, atthh, ggh, dhr, sfff, dgdfg, mnaoj]
13
  • thanks YCF_L, now am having my input like "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj" i need to split string and numeric separately Commented May 10, 2017 at 9:42
  • why you using matcher here Commented May 10, 2017 at 10:28
  • 1
    because there are no clear pattern your string is mix with numbers, so it is difficult to extract numbers and strings with the first solution, the better choice is to use Patterns @ManojKumarGovindharaj Commented May 10, 2017 at 10:30
  • now i tried to split the value like [12124, 1234123, 1234] and [astv, atthhggh, dhrsfff, dgdfg, mnaoj] i tried along with pattern but output doesn't spotted Commented May 11, 2017 at 12:27
  • what did you mean, @ManojKumarGovindharaj ? Commented May 11, 2017 at 12:35
1

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
  • thanks Alexey if i an input like "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj" how i want to split it... i want an output like [astv,atthhggh,dhrsffff,dgdfg,mnaoj] [12,124,1234,123,1234] Commented May 10, 2017 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.