*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 4339
    
    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
 
Don't get me started about those stupid light bulbs.
 
subject: String.split
 
Similar Threads
Displaying the largest and smallest of three integers
System.out.printf
Refering to another class's instance of a different class, inside a class
urgent answer required!!
end of file indicator for more than one file