7 Replies Latest reply: Jun 20, 2007 3:31 AM by 807600 RSS

    Hi , How to use string tokeniser with multiple delimiters

    807600
      Hi All,

      I am writing a program wherein I use StringTokeniser to achieve the following output.. Please Help

      My input is
      String input = "1,2,3,4,5-9,10,11,12-18,19,20";

      output:
      1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20"

      I have written codes to do the same process but in bits i don't know how to combine all the code:
      public class StringDemo
      {
          public static void main(String[] args)
          {
              String input = "1,2,3,4,5-9,10,11,12-18,19,20";
              int len = input.length();
              char[] tempCharArray = new char[len];
      
              
              for (int i = 0; i < len; i++)
              {
                  tempCharArray[i] = input.charAt(i);
                  
              }
      
              System.out.println(tempCharArray);
          }
      }
      The output I am getting is :

      1,2,3,4,5-9,10,11,12-18,19,20


      and i have written another code too
      import java.util.StringTokenizer;
      
      public class SetOfNumbers2
      {
          static String s = "5-9";
      
          static String key, val;
      
          public static void main(String[] args)
          {
              // TODO Auto-generated method stub
              StringTokenizer st = new StringTokenizer(s, " -");
      
              while (st.hasMoreTokens())
              {
                  key = st.nextToken();
                  val = st.nextToken();
              }
              int min = Integer.parseInt(key);
              int max = Integer.parseInt(val);
              int diff = max - min;
              System.out.print(min);
              for (int i = 0; i < diff; i++)
              {
                  min++;
                  System.out.print(","+ min);
              }
      
          }
      
      }
      OUTPUT:
      5,6,7,8,9


      Now how to combine both the codes so that i can get the desired output:


      output:
      1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20"

      Please Help ...
        • 1. Re: Hi , How to use string tokeniser with multiple delimiters
          807600
          Just use your second approach, your string tokenizer will break at the right position for your min max calculation. If also add a ", " to the tokenizer it will break at every "," and every "-"; the last thing you'll have to do is create your string properly. In your code you create the string after tokenizing this won't work if you use the complete input string since then only the last key-value pair will be handled. Just move your calculation into the tokenizing block and use a preinitialized string to save your result. You'll have to care about the initial case where the string is emtpy.
          I modified you code a little bit:
          import java.util.StringTokenizer;
          
          public class SetOfNumbers2
          {
               static String s = "1,2,3,4,5-9,10,11,12-18,19,20";
          
               static String key, val;
          
               public static void main(String[] args)
               {
                    // TODO Auto-generated method stub
                    StringTokenizer st = new StringTokenizer(s, ", -");
                    String result = "";
                    while (st.hasMoreTokens())
                    {
                         key = st.nextToken();
                         val = st.nextToken();
                         int min = Integer.parseInt(key);
                         int max = Integer.parseInt(val);
                         int diff = max - min;
                         for (int i = 0; i <= diff; i++)
                         {
                              result += (result.length() == 0) ? min : ("," + min);
                              min++;
                         }
                    }
          
                    System.out.println(result);
               }
          }
          In general you should add some kind of exception handling when converting strings to integers, i.e. if you can't ensure that your String is well formatted according to your assumptions.
          • 2. Re: Hi , How to use string tokeniser with multiple delimiters
            807600
                      StringBuffer buf = new StringBuffer();
                      String input = "1,2,3,4,5-9,10,11,12-18,19,20";
                      StringTokenizer st = new StringTokenizer(input, ",-", true);   // return the delim, because we need it to calc the range
                      String token     = null;
                      String prevToken = null;
                      while (st.hasMoreTokens()){
                           token = st.nextToken();
                           if (token.equals("-")){
                                token = st.nextToken();
                                int min = Integer.parseInt(prevToken) + 1;
                               int max = Integer.parseInt(token);
                               System.out.println(min + "," + max);
                               for (int i = min; i <= max; i++){
                                    buf.append(",").append(i);
                               }
                               prevToken = null;
                           }
                           else{
                                buf.append(token);
                                prevToken = token;
                           }
                      }
                      
                      System.out.println(buf.toString());
            • 3. Re: Hi , How to use string tokeniser with multiple delimiters
              807600
              Thanks a lot ...

              I tried and It works perfectly fine..

              I have few more doubts related to other topics So, i ll be posting new topics..
              Please Help..
              • 4. hey i am new to java
                807600
                can u help me out ??
                how to start with java
                wat should i install fro runnning java
                i am using windows xp professoinal
                i know few basic concepts
                but dunt know to put togethere ?
                do reply
                • 5. Re: Hi , How to use string tokeniser with multiple delimiters
                  807600
                  I want to convert date to words but i don't want to use in date or calender built functions

                  My input:
                  20/08/2007

                  Desired output:
                  20th June, 2007

                  I have written a code using StringTokeniser :


                  import java.util.StringTokenizer;

                  public class DateConversion
                  {
                  static String inputdate = "20/06/2007";

                  static String date, month, year;

                  public static void main(String[] args)
                  {

                  // TODO Auto-generated method stub
                  StringTokenizer st = new StringTokenizer(inputdate, " /");
                  while (st.hasMoreTokens())
                  {
                  System.out.println(st.nextElement());
                  }

                  }

                  }





                  The output I am getting now is

                  20
                  06
                  2007

                  but I want my output to be like...

                  20th June, 2007

                  Please help...
                  • 6. Re: hey i am new to java
                    807600
                    Hi ,

                    I would suggest you to go through this site,

                    http://java.sun.com/docs/books/tutorial/
                    • 7. Re: hey i am new to java
                      807600
                      If you want the output to be 20th June 2007 you'll have to define some conversion methods, e.g use the switch case for the month
                      switch (valueMonth)
                      {
                        case 1:
                          result += " January ";
                          break;
                        case 2:
                        .....
                      A similar method can be used for the days with the "th" as default and cases for 1,2,3.
                      Just define a resultstring before you tokenize and then print out the result after the tokinzing is complete