how to split a string and set to an object in java

Hi Experts,

String urls ="http:helloworld.com;http:helloworld2.com;http:hellloworld3.com"

the above urls string getting from db.
this column might have null value.Some time it might have one or two reference urls.
maximum referce urls will be 3.


i have to split and set this to an object.

String urls ="http:helloworld.com;http:helloworld2.com;http:hellloworld3.com";
Reference ref = new Reference()
ref.setreferenceUrl("http:helloworld.com")
ref.setreferenceUrl2("http:helloworld2.com")
ref.setReferenceUrl3(http:helloworld2.com);

Open in new window


Some one suggest me how to do.

Thanks,
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim CakalicSenior Developer/ArchitectCommented:
String.split is the easiest way I know to do this:
        String urls ="http:helloworld.com;http:helloworld2.com;http:hellloworld3.com";
        Reference ref = new Reference();
        if (urls != null && urls.length() > 0) {
            String[] refs = urls.split(":");
            for (int i = 0; i < refs.length; ++i) {
                switch (i) {
                    case 0: ref.setReferenceUrl(refs[0]); break;
                    case 1: ref.setReferenceUrl2(refs[1]); break;
                    case 2: ref.setReferenceUrl3(refs[2]); break;
                    default: break;
                }
            }
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
Jim, won't that make the URLs look like these  . . .

http
helloworld.com;http
helloworld2.com;http
hellloworld3.com


?
Amitkumar PSr. ConsultantCommented:
use URLs.split(";")
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Jim CakalicSenior Developer/ArchitectCommented:
Yeah. Sorry. Typo. Finger must have stuck on the Shift key. Thanks for the correction.
srikoteshAuthor Commented:
Hi Experts,

Thanks, I am getting the expected output.

Is there any alternative instead of switch?

Thanks,
krakatoaCommented:
You could use if statements instead.
Jim CakalicSenior Developer/ArchitectCommented:
Yes, you could used a series of if statements. Since you don't know how long the array returned by split will be you need to either prevent an ArrayIndexOutOfBounds or catch it. I apologize in advance. I'm typing this on my phone.
if (refs.length >0) {
    ref.setReferenceUrl(refs[0]);
}
if (refs.length >1) {
    ref.setReferenceUrl2(refs[1]);
}
if (refs.length > 2) {
    ref.setReferenceUrl3(refs[2]);
}

Open in new window

or
try {
    ref.setReferenceUrl(refs[0]);
    ref.setReferenceUrl2(refs[1]);
    ref.setReferenceUrl3(refs[2]);
} catch (ArrayIndexOutOfBoundsException e) {
    // ignored
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.