Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple stringTokenizer problem  RSS feed

 
srinivas krishnan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
I have a string with positive numbers and negative numbers, I am trying to get the positive and negative numbers in two separate ArrayList..and its getting messy..what Is it I am doing wrong..?Can someone suggest a better way.I am first tokenzing based on + and then tokenizing the tokenized string for "-". But the first one in the "-" would be a positive..and I am unable to get that element.
So string like "+1-2-4-8-9+6-4+56";
I need 1,6,56 in one ArrayList and 2,4,8,9,4 in another list. The String always start with + but after that there is no pattern, there could be no - numbers or there could be numerous - numbers. Can someone help me out..?

private void parseNumbers()
{
this.numbers="+1-2-4-8-9+6-4+56";
ArrayList positiveList = new ArrayList();
ArrayList negativeList = new ArrayList();
if (this.numbers !=null && !this.numbers.equalsIgnoreCase(""))
{
StringTokenizer tokenizer = new StringTokenizer(numbers,"+");
while (tokenizer.hasMoreTokens())
{
String numbers = tokenizer.nextToken();
System.out.println("Numbers that start with positive are"+ numbers);
StringTokenizer negNumbers = new StringTokenizer(numbers,"-");
while (negNumbers.hasMoreTokens())
{
String negNumber = ???
}
}
}

}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
srinivas krishnan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
Thanx. That works. But does it work only for numbers?? It doesnt seem to work If I have a String like
String text = "+x-y+d-e";
thanx again
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works for numbers only. The \\d is the pattern for a digit
replaceAll("-\\d+","")
To work with alpha, change \\d to
[a-zA-Z]
 
srinivas krishnan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mike,
Thanks. Actually i tried \\w and it works too. Just when I thought its looking great..i have a new problem..as we are using jdk131 for the app server..it gave me a No such method error. So I realized the method replaceAll with support to regex is only in jdk141..As such I cannot use this method. We cannot change our env to jdk141 ASAP..Can you suggest an alternative way to solve the problem.
Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This needs a fair bit of testing
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivas,
For jdk before 1.4 you can use another approach :

StringTokenizer st = new StringTokenizer(numbers,"-+",true);
the 3rd parameter indicates delimiters will also returned as tokens, you can then use

to arrange the numbers into list
HTH
[ March 17, 2004: Message edited by: chi Lin ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivas,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!