Having trouble with a .split("\\") method.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazedaces
    Contributor
    • May 2007
    • 284

    Having trouble with a .split("\\") method.

    Hello again. I'm trying to take as an input an ArrayList<Strin g> and utilize String's .spit(delimiter ) method to turn that into a String[][]. I'm getting some kind of error though (I'll post the code and error lower down).

    Here's a simple example:

    Code:
    Input ArrayList would be:
    ArrayList<String> temp = new ArrayList<String>(2);
        	temp.add("headerTest\\headerSectionTest\\");
        	temp.add("headerTest\\start-time\\sameLastPartTest");
        	temp.add("headerTest\\end-time\\sameLastPartTest");
    		temp.add("DataTest\\TimeTest\\");
    		temp.add("DataTest\\PieceOfDataTest\\");
    
    The String[][] that I want would be (assuming my code works):
    String[][] splittedStrings = new splittedStrings[temp.size()][];
    splittedStrings[0] = { "headerTest", "headerSectionTest" };
    splittedStrings[1] = { "headerTest", "start-time", "sameLastPartTest" };
    splittedStrings[2] = { "headerTest", "end-time", "sameLastPartTest" };
    splittedStrings[3] = { "DataTest", "TimeTest" };
    splittedStrings[4] = { "DataTest", "PieceOfDataTest" };
    Here's my code (main method and the table implementation method where I'm using the .spit("\\") method):

    [code=java]
    public static void main(String args[]) {
    ArrayList<Strin g> temp = new ArrayList<Strin g>(2);
    temp.add("heade rTest\\headerSe ctionTest\\");
    temp.add("heade rTest\\start-time\\sameLastP artTest");
    temp.add("heade rTest\\end-time\\sameLastP artTest");
    temp.add("DataT est\\TimeTest\\ ");
    temp.add("DataT est\\PieceOfDat aTest\\");

    runTCW rTCW = new runTCW(temp);
    rTCW.start();
    try {
    rTCW.join();
    } catch (InterruptedExc eption e) {
    e.printStackTra ce();
    }
    }
    [/code]

    [code=java]
    public MyTableModel(Ar rayList<String> al) {
    numRows = al.size();
    data = new Object[numRows][numCols];

    String[][] tagsSeparated = new String[numRows][];

    for (int i = 0; i < numRows; i++) {
    tagsSeparated[i] = al.get(i).split ("\\"); //This is where the error is reported, where I use the .split("\\") method
    }

    for (int i = 0; i < numRows; i++) {
    data[i][0] = new Boolean(true);

    data[i][1] = al.get(i);

    data[i][2] = al.get(i);
    Utilities.print (tagsSeparated[i]);

    data[i][3] = al.get(i);
    }
    }
    [/code]

    And the error it spits out:

    Code:
    Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
    \
     ^
        at java.util.regex.Pattern.error(Pattern.java:1700)
        at java.util.regex.Pattern.compile(Pattern.java:1453)
        at java.util.regex.Pattern.<init>(Pattern.java:1130)
        at java.util.regex.Pattern.compile(Pattern.java:822)
        at java.lang.String.split(String.java:2293)
        at java.lang.String.split(String.java:2335)
        at TagChoosingWindow$MyTableModel.<init>(TagChoosingWindow.java:210)
        at TagChoosingWindow.<init>(TagChoosingWindow.java:39)
        at runTCW.<init>(runTCW.java:14)
        at TagChoosingWindow.main(TagChoosingWindow.java:157)
    
    Process completed.
    I don't completely understand this error. Any and all help is much appreciated.

    -blazed
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You need four backslashes to match \ because it is a special-special character.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      You need four backslashes to match \ because it is a special-special character.
      Just to elaborate on r025198x's awfully correct and awfully terse reply:

      Suppose you want to split on a single backslash; you have to escape it with
      another backslash because (as r035198x already correctly wrote), the backslash
      is a special character for the regular expression compiler.

      If you want to supply that regular expression as a literal String, you have to get
      it past the Java compiler too but javac considers a backslash character as a
      special character as well, so basically you want to get two backslashes past
      javac; that takes one extra backslash each:

      [code=java]
      \\\\
      [/code]

      after javac munched on this string, all that is left is this:

      [code=java]
      \\
      [/code]

      and this is what the regular expression compiler turns it into

      [code=java]
      \
      [/code]

      I deliberately left out the double quotes for no particular reason just to emphesize
      on what both compilers actually see and work on.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Just to elaborate on r025198x's awfully correct and awfully terse reply:

        Suppose you want to split on a single backslash; you have to escape it with
        another backslash because (as r035198x already correctly wrote), the backslash
        is a special character for the regular expression compiler.

        If you want to supply that regular expression as a literal String, you have to get
        it past the Java compiler too but javac considers a backslash character as a
        special character as well, so basically you want to get two backslashes past
        javac; that takes one extra backslash each:

        [code=java]
        \\\\
        [/code]

        after javac munched on this string, all that is left is this:

        [code=java]
        \\
        [/code]

        and this is what the regular expression compiler turns it into

        [code=java]
        \
        [/code]

        I deliberately left out the double quotes for no particular reason just to emphesize
        on what both compilers actually see and work on.

        kind regards,

        Jos
        Don't you dare disrespect me by calling me r025198x. I've long since moved up to the r03 class.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Don't you dare disrespect me by calling me r025198x. I've long since moved up to the r03 class.
          Sorry, that's what your name looks like in my private unodecosimal radix number
          system. It's quite a complicated system so I'll explain it some other time ;-)

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Sorry, that's what your name looks like in my private unodecosimal radix number
            system. It's quite a complicated system so I'll explain it some other time ;-)

            kind regards,

            Jos
            You need to upgrade your system then. It's probably version 0.1 and is written in Fortan right?

            Comment

            • blazedaces
              Contributor
              • May 2007
              • 284

              #7
              It worked obviously. Thank you so much for the help, both of you.

              -blazed

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by blazedaces
                It worked obviously. Thank you so much for the help, both of you.

                -blazed
                One more post blazed, and you're seating pretty on 200 posts. I'll see If I can get a present for you.

                Comment

                • blazedaces
                  Contributor
                  • May 2007
                  • 284

                  #9
                  Originally posted by r035198x
                  One more post blazed, and you're seating pretty on 200 posts. I'll see If I can get a present for you.
                  It's funny you should say that, my birthday was last monday... my friends got me Frank Miller's (guy who wrote sin city comics) Comic of 300 (300 is only one digit off of 200, funny eh? Not really...). I didn't even know that movie was based on a comic. I really enjoyed it...

                  Anyway, ya, thanks, didn't even notice my post count was getting there...

                  Well to the brave 200 ... eh ... 300 ... eh ... whatever,

                  -blazed

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    A bit belated but nevertheless: Happy Birthday to you.

                    kind regards,

                    Jos

                    Comment

                    • blazedaces
                      Contributor
                      • May 2007
                      • 284

                      #11
                      Originally posted by JosAH
                      A bit belated but nevertheless: Happy Birthday to you.

                      kind regards,

                      Jos
                      Thank you good sir.

                      -blazed

                      Comment

                      • hickmmi
                        New Member
                        • Apr 2008
                        • 1

                        #12
                        Thank you both for answering and explaining the problem. Saved me a lot of time!

                        Regards,

                        Matt

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by hickmmi
                          Thank you both for answering and explaining the problem. Saved me a lot of time!

                          Regards,

                          Matt
                          Thanks for the thanks.

                          Comment

                          Working...