# Parsing or Tokenizer??

• 05-27-2008, 07:24 PM
hiklior
Parsing or Tokenizer??
Example

000520000000023000210000000089

I am fairly new to java and i would appreciate if somebody could help me with this. I have series to numbers just like above . I need to divide this series of numbers into chunk of 5 numbers and find a chunk that has 5 zeros preceeded by or followed by chunk of five numbers greater than or equal to 50.

Lets divide the above example into chunk

00052 00000 00023 00021 00000 00089

As you could see 00052 ( which is greater than 50 ) is followed by 00000 it meets our criteria so print 00000
Also 000089 has 00000 in front of it also meets our criteria.

I would appreciate if anybody could help me with this.

Hik
• 05-27-2008, 07:40 PM
Chris.Brown.SPE
The way i would do this is:

1. Get the number in string format.
2. Using substring, get the first 5 numbers and leave the rest.
3. Repeat until all are in groups of 5 in an array or something.
4. Convert them to integers for easy comparison.
5. Go through each item in the array to see if it is greater than 50.
6. If it is greater than 50, check the item before and after it to see if it is equal to zero.

Let me know if you need any more help on this.
• 05-27-2008, 07:44 PM
hiklior
Well, that sounds just like what i need but i am just taking baby steps with programming I need more help. Thank you so much for quick response anyways.
Hik
• 05-27-2008, 07:48 PM
Chris.Brown.SPE
Let me know if you get stuck on the programming. I would just drop it all on here for you, but then you wouldn't learn anything would ya?
• 05-28-2008, 04:14 AM
Eranga
Chris gave all required steps for you. So start from there. The steps explained by Chris is much much much easier to understand. Try and if you stuck with something ask here.

Forum is not a place where you can find free codes. It's a place to learn. People are ready to help you in friendly manner. So start work my friend. :)
• 05-28-2008, 04:27 AM
Zosden
I personally would use the awesome power of recursion to deal with this problem.
• 05-28-2008, 04:41 AM
Chris.Brown.SPE
Recursion? Are you serious? That would be outrageous overkill for this. And the calls added to the stack would prolly slow it down more than it could help.
• 05-28-2008, 04:44 AM
Eranga
Yep, it's a dead for me. I don't know it is possible if the string is too long, around thousand characters.
• 05-28-2008, 04:50 AM
Zosden
Java without extending the size of java space for stacks you can have thousands of recursive calls. If he is parsing a string that big than their is a problem. But the point of recursion is that it is extremly easy to implement and plus it isn't that much slower just a little bit more overhead. I would deffenently use recursion for this problem.
• 05-28-2008, 05:07 AM
Chris.Brown.SPE
I challenge you. This is the most straight forward problem i've seen in a while. I could write this loop in less than 10 lines and still have it faster than anything you could do recursively. I love recursion myself, but this is no place for it.
• 05-28-2008, 05:40 AM
Eranga
Wow, great stuff here foes. ;) In this point too, I agreed with Chris. Point is this Zosden, you said that recursion is easy to implement. I don't think so at all. For me there is a big difference.
• 05-28-2008, 05:50 AM
Chris.Brown.SPE
Recursion, however trivial it may be for the more advanced programmer, can be very very complicated for a beginner. We have to remember this especially when in the "New To Java" section. I think recursion is cake, but that is all experience. The great tragedy of forums, you never really know what the person asking the question knows.
• 05-28-2008, 06:05 AM
Eranga
Yes Chris. But actually I'm not worried about the forum category, weather it's "New To Java" or not. But now I have to think something on your comment. :)

What I think is, to implement recursion need to make more effort. Even I take that much effort comparing the performance simpler way is much better in most cases, like said for long strings. What you think of that. Agreed with me or not.
• 05-28-2008, 01:04 PM
Chris.Brown.SPE
On this particular problem I am agreeing 100%. But there are many problems out there that are on the edge of whether or not to use recursion. On those i would prolly go for it.
• 05-28-2008, 01:53 PM
Zosden
I think recursion would be easier because all he would have to do is take the steps you guys have given him as the ending point for the recursive call. What you would do is say pass in the whole string. Then check to see if the string is less than or equal to 5 than start your checking if not then recursively call the method with the first 5 letters than also have a call that recursively calls the rest of the string. How hard was that.
• 05-28-2008, 02:20 PM
Chris.Brown.SPE
Ok, i just cant take it anymore. Here's my code. Please show me your amazing solution using recursion.

Code:

```        static void doit(String temp) {                 while (temp.length() > 5) {                         int var1 = Integer.parseInt(temp.substring(0, 5));                         temp = temp.substring(5);                         int var2 = Integer.parseInt(temp.substring(0, 5));                         if (var1 > 50 & var2 == 0) {                                 System.out.println(var1);                         } else {                                 if (var1 == 0 & var2 > 50) {                                         System.out.println(var2);                                 }                         }                 }         }```
This will output all of the numbers that qualify.