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?
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

0

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


?
0
Amitkumar PSr. ConsultantCommented:
use URLs.split(";")
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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

Thanks, I am getting the expected output.

Is there any alternative instead of switch?

Thanks,
0
krakatoaCommented:
You could use if statements instead.
0
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

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.