This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 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: 629

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: 4244
    
    7

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: 629

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: 629

Welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.split
 
Similar Threads
Refering to another class's instance of a different class, inside a class
urgent answer required!!
Displaying the largest and smallest of three integers
System.out.printf
end of file indicator for more than one file