Split and pass empty strings

Posted on 2006-03-27
Last Modified: 2010-04-01
I have the following code that bombs if rawClassification = "::"; How do I get it to pass two empty strings in this case?

String[] myClassificationPieces = rawClassification.split("::");
      request.setAttribute("classification_category" , myClassificationPieces[0]) ;
      request.setAttribute("classification_subcategory" , myClassificationPieces[1]) ;
Question by:894359
    1 Comment
    LVL 29

    Accepted Solution

    You need to check the length of the array before you refer to them by direct index like myArray[5] else you will get an ArrayOutOfBoundsException as you are probably seeing

    String[] myClassificationPieces = rawClassification.split("::");
    if (myClassificationPieces.length >= 2 ){
         request.setAttribute("classification_category" , myClassificationPieces[0]) ;
         request.setAttribute("classification_subcategory" , myClassificationPieces[1]) ;

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    It Is not possible to enable LLDP in vSwitch(at least is not supported by VMware), so in this article we will enable this, and also go trough how to enabled CDP and how to get this information in vSwitches and also in vDS.
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now