This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String.split Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String.split" Watch "String.split" New topic
Author

String.split

Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

For some reason, I can't seem to get String.split to work for me. What am I doing wrong?

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

Hi Eden, on line 10-12 you had declared string as null and therefore if condition will never be true and in else you assign them as "null" hence you got output as null null null. What you want to do?


Life is easy because we write the source code.....
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4541
    
    8

Are you sure you're testing what you think you're testing in those if statements? For example, you set first to null, then check if it's null - unsurprisingly, it is.
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Oh. Whoopsy, then. Yes, this isn't what I wanted to check. Though, now that I did, I get a ArrayIndexOutOfBoundsException. Why's that?

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

Because size of String[] input is 3 and you are accessing index 3. Array index start from 0. Here size is 3 so you can access index from 0 to 2.
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Wow.
I forgot about that completely! Thank you all very much.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

Welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.split
 
It's not a secret anymore!