Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9232

Re: How to check if the n-th part of an array of Strings exist?

From Wojtek <nowhere@a.com>
Newsgroups comp.lang.java.programmer
Subject Re: How to check if the n-th part of an array of Strings exist?
Date 2011-10-26 23:37 -0700
Organization NewsGuy - Unlimited Usenet $19.95
Message-ID <mn.d5897dba40f6378a.70216@a.com> (permalink)
References <4ea6e340$0$6580$9b4e6d93@newsspool3.arcor-online.net>

Show all headers | View raw


Jochen Brenzlinger wrote :
> Assume I have an array of Strings like:
>
> String[] part = .....;
>
> If I try to access e.g. the 5th part and it does not exist then java crashes 
> with an Indexouitofbounds exception.
>
> How can I most easily check (in a bigger logical expression) whether the 5th 
> slot exist?
>
> The following does not work:
>
> if (part[4].exist() && part[4].equals("hello")) {
>    ....
>    }
>    
> This does not work either:
>
> if (part[4].length() > 0 && part[4].equals("hello")) {
>    ....
>    }
>

You check for the number of "slots" in the array:

if ( index >= 0 && index < part.length )
  do something with part[index]
else
  error();

-- 
Wojtek :-)

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to check if the n-th part of an array of Strings exist? jochen2@brenz.com (Jochen Brenzlinger) - 2011-10-25 16:26 +0000
  Re: How to check if the n-th part of an array of Strings exist? Tom McGlynn <taqmcg@gmail.com> - 2011-10-25 09:41 -0700
  Re: How to check if the n-th part of an array of Strings exist? Wojtek <nowhere@a.com> - 2011-10-26 23:37 -0700
    Re: How to check if the n-th part of an array of Strings exist? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-10-27 10:20 +0300
      Re: How to check if the n-th part of an array of Strings exist? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-27 07:26 -0400
        Re: How to check if the n-th part of an array of Strings exist? Tim Slattery <Slattery_T@bls.gov> - 2011-10-27 09:09 -0400
          Re: How to check if the n-th part of an array of Strings exist? Mayeul <mayeul.marguet@free.fr> - 2011-10-27 15:45 +0200
            Re: How to check if the n-th part of an array of Strings exist? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-10-27 16:55 +0300
            Re: How to check if the n-th part of an array of Strings exist? Gene Wirchenko <genew@ocis.net> - 2011-10-27 10:37 -0700
              Re: How to check if the n-th part of an array of Strings exist? Tassilo Horn <tassilo@member.fsf.org> - 2011-10-27 21:31 +0200
            Re: How to check if the n-th part of an array of Strings exist? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-27 21:25 -0400
              Re: How to check if the n-th part of an array of Strings exist? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-10-28 10:08 +0300
  Re: How to check if the n-th part of an array of Strings exist? Roedy Green <see_website@mindprod.com.invalid> - 2011-10-28 06:20 -0700
  Re: How to check if the n-th part of an array of Strings exist? Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 14:54 -0500

csiph-web