Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 999021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:17:16+00:00 2026-05-16T07:17:16+00:00

I am trying to convert a String into an ArrayList. For example, my Struts2

  • 0

I am trying to convert a String into an ArrayList. For example, my Struts2 webapp returns this String named row in a format similar to this:

[A, BB, CCC, DDDD, 1, 0, 1] (something along those lines)

I need to convert them into an ArrayList so I can prepopulate some forms in another JSP page. I hardcoded a method to convert such Strings into list form:

        StringBuffer rowBuffer = new StringBuffer(row);
        int startIndex = 0;
        int endIndex = rowBuffer.indexOf(",") - 1;
        rowBuffer.deleteCharAt(rowBuffer.indexOf("["));
        rowBuffer.deleteCharAt(rowBuffer.indexOf("]"));
        while(startIndex != -1 && endIndex != -1 && startIndex < endIndex)
        {
          String subString = rowBuffer.substring(startIndex, endIndex);

       if(subString.contains(","))
       {
          rowList.add(" ");
          startIndex = endIndex + 1;
          endIndex = rowBuffer.indexOf(",", startIndex);
       }

       else
       {
        rowList.add(subString);
        startIndex = endIndex + 2;
        endIndex = rowBuffer.indexOf(",", startIndex + 1);
       }

       if(endIndex == -1)
       {
          rowList.add(rowBuffer.substring(startIndex));
          break;
       }
    }

This works fine in cases where all the fields are populated. However, lets say I have a String that looks like this: [A, BB, , , 1, 0, 0] (the 3rd and 4th fields are missing), then I get something that doesn’t work (the blank elements don’t register correctly, and the size of the list is 6, when it should be 7). Is there a more elegant solution than hardcoding? If not, could someone point me in the right direction on how to handle cases with blank fields? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:17:16+00:00Added an answer on May 16, 2026 at 7:17 am

    Well I changed my code a bit and made it work (it’s probably far from the most elegant solution, but it works for me…

        StringBuffer rowBuffer = new StringBuffer(row);
        int startIndex = 0;
        int endIndex = rowBuffer.indexOf(",") - 1;
        rowBuffer.deleteCharAt(rowBuffer.indexOf("["));
        rowBuffer.deleteCharAt(rowBuffer.indexOf("]"));
        while(startIndex != -1 && endIndex != -1 && startIndex < endIndex)
        {
          String subString = rowBuffer.substring(startIndex, endIndex);
    
           if(subString.contains(","))
           {
              rowList.add(" ");
              startIndex = endIndex - 1;
              endIndex = rowBuffer.indexOf(", ", startIndex + 1);
           }
    
           else
           {
             if(subString.equals("1"))
                 rowList.add("True");
             else if(subString.equals("0"))
                 rowList.add("False");
             else
              rowList.add(subString);
            startIndex = endIndex + 2;
            endIndex = rowBuffer.indexOf(",", startIndex + 1);
           }
    
           if(endIndex == -1)
           {
              if(subString.equals("1"))
                 rowList.add("True");
              else if(subString.equals("0"))
                 rowList.add("False");
              break;
           }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to convert a date string into an age. The string is like:
I've been trying to convert a date value into a more readable format. To
I'm trying to convert an incoming sting of 1s and 0s from stdin into
I am trying to take the string <BR> in VB.NET and convert it to
I am trying to convert an ASP.NET website into a web application project. The
I am trying to convert an int into three bytes representing that int (big
i am currently trying to convert an integer into a list. E.g. 1234 =>
I'm trying to convert some existing C# code into a CLR stored procedure. The
I'm trying to convert a time info I reveive as a UTC string to
I am trying to implement a function called inet_pton which will convert a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.