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

    Default Help with substring

    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


    This is what i have upto now and i get error message saying "out of bound". I would also like to credit Chris.Brown SPE for this code

    import java.util.regex.*;
    import java.io.*;

    class readlinebyline2
    {
    public static void main(String args[])
    {
    try{

    FileInputStream fstream = new FileInputStream("c:/input.doc");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)
    {


    while (strLine.length() >= 5)
    {
    int var1 = Integer.parseInt(strLine.substring(0, 5));
    strLine = strLine.substring(5);
    int var2 = Integer.parseInt(strLine.substring(0, 5));
    if (var1 > 50 & var2 == 0)
    {
    System.out.println(var1);
    }
    else
    {
    if (var1 == 0 & var2 > 50)
    {
    System.out.println(var2);
    }
    }

    }

    }

    }
    catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    you are not iterating through the length of the string.

    Java Code:
    while (strLine.length() >= 5) {
           int var1 = Integer.parseInt(strLine.substring(0, 5));
        ...
    }
    needs something like

    Java Code:
    for (int i = 0; i < strLine.length(); i += 5) {
      String sub = strLine.substring(i, i+4);
      int var1 = Integer.parseInt(sub);
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    cross to separate help sites!?!?!
    Wow, what a jerk

  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
    20

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class SubstringTest {
        public static void main(String[] args) {
            String s = "000520000000023000210000000089";
            String[] blocks = new String[s.length()/5];
            for(int i = 0, pos = 0; i < blocks.length; i++) {
                blocks[i] = s.substring(pos, pos+5);
                pos += 5;
            }
            for(int i = 0; i < blocks.length; i++) {
                if(qualifies(i, blocks)) {
                    System.out.printf("blocks[%d] = %s%n", i, blocks[i]);
                }
            }
        }
    
        private static boolean qualifies(int index, String[] blocks) {
            boolean before = (index == 0) ? false :
                                Integer.parseInt(blocks[index-1]) >= 50;
            boolean after = (index == blocks.length-1) ? false :
                                Integer.parseInt(blocks[index+1]) >= 50;
            return before || after;
        }
    }

  7. #7
    Alan-LB's Avatar
    Alan-LB is offline Member
    Join Date
    Jun 2008
    Location
    Junee, NSW, Australia
    Posts
    19
    Rep Power
    0

    Default

    The posting in the other forum is different!!!

    Alan.
    There are 10 types of people - those who understand binary and those who don't!!
    Today is the Beta version of Tomorrow!!

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    You can use String Tokernizer class too. I really like that. :)
    Sun now discourage the use of StringTokenizer and recommend String.split(..) or the regex package. See the StringTokenizer JavaDocs.

    The best way to get a good idea is to get a lot of ideas...
    L. Pauling

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

    Default

    As far as I know it's used around 1.0 and now I'm working on 1.6. But still not found such thing on that. I used in more cases. Major reason is it's much simpler in simple applications.

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    StringTokenizer is expected to be deprecated soon, so String.split is much prefered, and its not much more complex, split() also has the advantage of not needing the enumeration pointer, which is a bit klunky.

  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
    20

    Default

    Do you know any reason to deprecate it? As said, use of split() is not complex. What I want to say is, depend on the application complexity I choose one of them. StringTotkenizer is easy to use in simple cases.

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Sorry, no. Perhaps it doesn't work well in parallel threads, this is just a guess, I don't try to keep up with the JSR process.

    Its a bit ugly, and enumerations are 'disfavored' and the iterator is preferred these days.

  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
    20

    Default

    I agreed. In few months back I try to do it an application, which are working with parallel threads. It freeze the application because of the heap size issues and so. I think may be it have keep all tokens to workout.

Similar Threads

  1. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  2. How to use subString method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  3. help me with a realy easy program (substring)
    By michcio in forum New To Java
    Replies: 7
    Last Post: 01-27-2008, 12:41 AM
  4. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 PM

Posting Permissions

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