Results 1 to 16 of 16
  1. #1
    hiklior is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default 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

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

  3. #3
    hiklior is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    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

  4. #4
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. :)

  6. #6
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    I personally would use the awesome power of recursion to deal with this problem.
    My IP address is 127.0.0.1

  7. #7
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yep, it's a dead for me. I don't know it is possible if the string is too long, around thousand characters.

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    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.
    My IP address is 127.0.0.1

  10. #10
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  12. #12
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  14. #14
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    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.

  15. #15
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    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.
    My IP address is 127.0.0.1

  16. #16
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Ok, i just cant take it anymore. Here's my code. Please show me your amazing solution using recursion.

    Java 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.
    Last edited by Chris.Brown.SPE; 05-28-2008 at 02:22 PM.

Similar Threads

  1. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 11:07 AM
  2. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 AM
  3. Using SAXParserFactory for parsing XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-20-2008, 09:42 AM
  4. Parsing URL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •