Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Help with knowing if .split()[1] exists

  1. #1
    trath is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Help with knowing if .split()[1] exists

    I want to do that if somebody types "!say ABC" then it would be able to acess only "ABC" and do whatever to it.
    so I have an
    Java Code:
    str.indexOf("!say")
    , then to get "ABC" I do
    Java Code:
    str.split(" ")[1]
    The problem is that if somebody types only "!say" with nothing that follows, the program crashes due to "out of bounds" or w/e. (because I am trying to access "str.split(" ")[1]" which doesn't exist).
    What if statement can I use to check if ".split(" ")[1]" exists?


    Thanks

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Help with knowing if .split()[1] exists

    Java Code:
    String[] split = str.split(" ");
    if(split.length > 1) {
       // split[1] exists
    }
    Norm likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. call a thread knowing its ID
    By cucucur in forum Threads and Synchronization
    Replies: 1
    Last Post: 07-05-2011, 12:14 AM
  2. Need help in knowing how to clear buffer.
    By tedstevens in forum New To Java
    Replies: 1
    Last Post: 08-20-2009, 05:02 AM
  3. Knowing which IP address is active
    By sukatoa in forum Networking
    Replies: 2
    Last Post: 05-05-2009, 04:01 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •