Reputation: 119
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]
Upvotes: 4
Views: 389
Reputation: 60016
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]
Upvotes: 1
Reputation: 521979
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("\\*");
Upvotes: 3
Reputation: 17721
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);
}
Upvotes: 1
Reputation: 393936
You should create two List
s 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.
Upvotes: 2