8 Replies Latest reply: Jun 27, 2010 11:44 AM by 843789 RSS

    StringTokenizer

    843789
      Hello,

      I am banging my head on this for awhile now please help. I am passing this string to my action class String strTemp = "12,5|10,7|44,5|........"; inside my action class I would like iterate through the string and assign the value to an object. For example 12,5 represent person id, and person score, and 10,7 also represent a person object etc..... I tried different methods with lo luck.

      Thanks,
      L
        • 1. Re: StringTokenizer
          mycoffee
          jayden01 wrote:
          Hello,

          I am banging my head on this for awhile now please help. I am passing this string to my action class String strTemp = "12,5|10,7|44,5|........"; inside my action class I would like iterate through the string and assign the value to an object. For example 12,5 represent person id, and person score, and 10,7 also represent a person object etc..... I tried different methods with lo luck.

          Thanks,
          L
          Use
          String[] yourArray = strTemp.split("|"); should work
          • 2. Re: StringTokenizer
            843789
            Thanks! If do that than how do I know which value is the person id and person score.

            -L
            • 3. Re: StringTokenizer
              DrClap
              So you have "12,5" and you need to split that into two parts? Hint: there's a method in the String class for that. Hint: it's already been mentioned in this thread.
              • 4. Re: StringTokenizer
                843789
                Thank you for the reply, I will try that.

                -J
                • 5. This Thread is now moved
                  Darryl Burke
                  Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
                  • 6. Re: StringTokenizer
                    843789
                    Be sure to read the documentation on split().
                    Read all of the words.
                    It will be just a little more complex then you might expect at first.
                    Not a lot more complex just a little.
                    When you don't get the results you expected, don't give up, read the docs again, this time read ALL of the words.
                    • 7. Re: StringTokenizer
                      699554
                      An alternative solution to splitting input multiple times is to use regular expressions:
                      public class Foo {
                      
                           public static void main(String[] args) {
                                List<Node> nodes = new Foo().extractNodes("12,5|10,7|44,5");
                                
                                for(Node node : nodes) {
                                     System.out.println(node);
                                }
                           }
                           
                           private Pattern pattern = Pattern.compile("(\\d+),(\\d+)");
                           
                           public List<Node> extractNodes(final String string) {
                                List<Node> nodes = new ArrayList<Node>();
                                Matcher matcher = pattern.matcher(string);
                                
                                while(matcher.find()) {
                                     Integer num1 = Integer.parseInt(matcher.group(1));
                                     Integer num2 = Integer.parseInt(matcher.group(2));
                                     nodes.add(new Node(num1, num2));
                                }
                                return nodes;
                           }
                      
                           public static class Node {
                                public Node(final Integer number1, final Integer number2) {
                                     num1 = number1;
                                     num2 = number2;
                                }
                                public Integer num1;
                                public Integer num2;
                                
                                @Override
                                public String toString() {
                                     return "Node[num1:=" + num1 + ", num2:=" + num2 + "]";
                                }
                           }
                      }
                      Output:

                      Node[num1:=12, num2:=5]
                      Node[num1:=10, num2:=7]
                      Node[num1:=44, num2:=5]

                      Mel
                      • 8. Re: StringTokenizer
                        843789
                        Thank you so much!!! I followed your example and it is working for me.

                        -J