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


Groups > comp.lang.java.programmer > #9175 > unrolled thread

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

Started byjochen2@brenz.com (Jochen Brenzlinger)
First post2011-10-25 16:26 +0000
Last post2011-11-06 14:54 -0500
Articles 14 — 11 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#9175 — How to check if the n-th part of an array of Strings exist?

Fromjochen2@brenz.com (Jochen Brenzlinger)
Date2011-10-25 16:26 +0000
SubjectHow to check if the n-th part of an array of Strings exist?
Message-ID<4ea6e340$0$6580$9b4e6d93@newsspool3.arcor-online.net>
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")) {
   ....
   }

So what else can I use?
 
Jochen

[toc] | [next] | [standalone]


#9176

FromTom McGlynn <taqmcg@gmail.com>
Date2011-10-25 09:41 -0700
Message-ID<ac1dd82d-4de2-473a-8470-f10d0d88ce02@q13g2000vbd.googlegroups.com>
In reply to#9175
On Oct 25, 12:26 pm, joch...@brenz.com (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")) {
>    ....
>    }
>
> So what else can I use?
>
> Jochen


Depending upon how careful you need to be you might try....

if (part != null         // The array is defined.
    && part.length > 4   // The array is long enough
    && part[4] != null   // The fifth! element is not null
    && part[4].equals("hello")) { // Do something knowing that the
string is there.
...

You can replace the last two with "hello".equals(part[4]) if you just
want to check for equality, but I find that idiom slightly
disconcerting.

 Tom McGlynn

[toc] | [prev] | [next] | [standalone]


#9232

FromWojtek <nowhere@a.com>
Date2011-10-26 23:37 -0700
Message-ID<mn.d5897dba40f6378a.70216@a.com>
In reply to#9175
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 :-)

[toc] | [prev] | [next] | [standalone]


#9234

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2011-10-27 10:20 +0300
Message-ID<qot62jahpek.fsf@ruuvi.it.helsinki.fi>
In reply to#9232
Wojtek writes:

> You check for the number of "slots" in the array:
> 
> if ( index >= 0 && index < part.length )
>   do something with part[index]
> else
>   error();

Shame that (0 <= index < part.length) means something useless instead.

[toc] | [prev] | [next] | [standalone]


#9238

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-10-27 07:26 -0400
Message-ID<j8bf5f$b8i$1@dont-email.me>
In reply to#9234
On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
> Wojtek writes:
>
>> You check for the number of "slots" in the array:
>>
>> if ( index>= 0&&  index<  part.length )
>>    do something with part[index]
>> else
>>    error();
>
> Shame that (0<= index<  part.length) means something useless instead.

     Be thankful that its meaning in Java is more useful than in C.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#9241

FromTim Slattery <Slattery_T@bls.gov>
Date2011-10-27 09:09 -0400
Message-ID<htlia7donph3mi48evn2l5qi2ini8rbn7a@4ax.com>
In reply to#9238
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:

>On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
>> Wojtek writes:
>>
>>> You check for the number of "slots" in the array:
>>>
>>> if ( index>= 0&&  index<  part.length )
>>>    do something with part[index]
>>> else
>>>    error();
>>
>> Shame that (0<= index<  part.length) means something useless instead.
>
>     Be thankful that its meaning in Java is more useful than in C.

Please explain that. I'm probably wrong, but I'd think they'd be the
same.

Evaluate one of the comparisons (not positive which one), resulting in
a boolean. Convert the boolean to a number to do the other comparison.
Not very useful.

-- 
Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt

[toc] | [prev] | [next] | [standalone]


#9243

FromMayeul <mayeul.marguet@free.fr>
Date2011-10-27 15:45 +0200
Message-ID<4ea95fbd$0$631$426a74cc@news.free.fr>
In reply to#9241
On 27/10/2011 15:09, Tim Slattery wrote:
> Eric Sosman<esosman@ieee-dot-org.invalid>  wrote:
>
>> On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
>>> Wojtek writes:
>>>
>>>> You check for the number of "slots" in the array:
>>>>
>>>> if ( index>= 0&&   index<   part.length )
>>>>     do something with part[index]
>>>> else
>>>>     error();
>>>
>>> Shame that (0<= index<   part.length) means something useless instead.
>>
>>      Be thankful that its meaning in Java is more useful than in C.
>
> Please explain that. I'm probably wrong, but I'd think they'd be the
> same.
>
> Evaluate one of the comparisons (not positive which one), resulting in
> a boolean. Convert the boolean to a number to do the other comparison.
> Not very useful.
>

I'd wager that refusing to compile is more useful than meaning something 
useless and confusing, therefore errors-prone.

--
Mayeul

[toc] | [prev] | [next] | [standalone]


#9244

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2011-10-27 16:55 +0300
Message-ID<qotd3dir12p.fsf@ruuvi.it.helsinki.fi>
In reply to#9243
Mayeul writes:
> On 27/10/2011 15:09, Tim Slattery wrote:
> > Eric Sosman  wrote:
> >
> >> On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
> >>> Wojtek writes:
> >>>
> >>>> You check for the number of "slots" in the array:
> >>>>
> >>>> if ( index>= 0&&   index<   part.length )
> >>>>     do something with part[index]
> >>>> else
> >>>>     error();
> >>>
> >>> Shame that (0<= index<   part.length) means something useless instead.
> >>
> >>      Be thankful that its meaning in Java is more useful than in C.
> >
> > Please explain that. I'm probably wrong, but I'd think they'd be the
> > same.
> >
> > Evaluate one of the comparisons (not positive which one),
> > resulting in a boolean. Convert the boolean to a number to do the
> > other comparison.  Not very useful.
> 
> I'd wager that refusing to compile is more useful than meaning
> something useless and confusing, therefore errors-prone.

Indeed, I missed that. I'll agree that Java's interpretation is less
harmful than C's. Python gets this right.

[toc] | [prev] | [next] | [standalone]


#9255

FromGene Wirchenko <genew@ocis.net>
Date2011-10-27 10:37 -0700
Message-ID<p85ja7hgsghec32c7ahkg2p6tss06n88lj@4ax.com>
In reply to#9243
On Thu, 27 Oct 2011 15:45:09 +0200, Mayeul <mayeul.marguet@free.fr>
wrote:

>On 27/10/2011 15:09, Tim Slattery wrote:
>> Eric Sosman<esosman@ieee-dot-org.invalid>  wrote:
>>
>>> On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
>>>> Wojtek writes:
>>>>
>>>>> You check for the number of "slots" in the array:
>>>>>
>>>>> if ( index>= 0&&   index<   part.length )
>>>>>     do something with part[index]
>>>>> else
>>>>>     error();
>>>>
>>>> Shame that (0<= index<   part.length) means something useless instead.
>>>
>>>      Be thankful that its meaning in Java is more useful than in C.
>>
>> Please explain that. I'm probably wrong, but I'd think they'd be the
>> same.
>>
>> Evaluate one of the comparisons (not positive which one), resulting in
>> a boolean. Convert the boolean to a number to do the other comparison.
>> Not very useful.

>I'd wager that refusing to compile is more useful than meaning something 
>useless and confusing, therefore errors-prone.

     It is neither useless nor confusing.  That notation is used in
math.  For example:
          Let x, y, and z be integers with x < y < z.
I think it clearer than
          Let x, y, and z be integers with x < y and y < z.
The verbosity does not gain anything.

     ISTR a programming language that did allow this construct.  I
would like to see it more commonly used.  How often is someone going
to do a less than or a greater than comparison on a boolean value?
OTOH, checking that a value is in a range is very common.

     x < y < z is shorter than SQL's
          a is between b and c
and allows for different operators (>/>= or </<=).  (between uses >=
and <= only.)

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#9257

FromTassilo Horn <tassilo@member.fsf.org>
Date2011-10-27 21:31 +0200
Message-ID<87hb2uut8a.fsf@thinkpad.tsdh.de>
In reply to#9255
Gene Wirchenko <genew@ocis.net> writes:

>>I'd wager that refusing to compile is more useful than meaning
>>something useless and confusing, therefore errors-prone.
>
>      It is neither useless nor confusing.  That notation is used in
> math.  For example:
>           Let x, y, and z be integers with x < y < z.
> I think it clearer than
>           Let x, y, and z be integers with x < y and y < z.
> The verbosity does not gain anything.
>
>      ISTR a programming language that did allow this construct.  I
> would like to see it more commonly used.

Nearly all Lisps support that.

,----[ Clojure ]
| user> (doc <)
| -------------------------
| clojure.core/<
| ([x] [x y] [x y & more])
|   Returns non-nil if nums are in monotonically increasing order,
|   otherwise false.
| nil
| user> (< 1 2.4 30 111 9e10)
| true
| user> (< 0)
| true ;; Of course, all nums are in monotonically increasing order
`----

,----[ Common Lisp ]
| * (describe #'<)
| 
| #<FUNCTION <>
|   [compiled function]
| 
| Lambda-list: (NUMBER &REST MORE-NUMBERS)
| Derived type: (FUNCTION (T &REST T) (VALUES (MEMBER NIL T) &OPTIONAL))
| Documentation:
|   Return T if its arguments are in strictly increasing order, NIL otherwise.
| Source file: SYS:SRC;CODE;NUMBERS.LISP
| * (< 1 10 16 99 189)
| 
| T
| * (< 1)
|
| T ;; Of course, all arguments are strictly in intreasing order
`----

,----[ Scheme ]
| #;> (< -1 0 19 93 321 1000)
| #t
| #;> (< 1)
| Error in <: incorrect number of arguments to procedure
`----

Bye,
Tassilo

[toc] | [prev] | [next] | [standalone]


#9267

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-10-27 21:25 -0400
Message-ID<j8d0c5$uj2$1@dont-email.me>
In reply to#9243
On 10/27/2011 9:45 AM, Mayeul wrote:
> On 27/10/2011 15:09, Tim Slattery wrote:
>> Eric Sosman<esosman@ieee-dot-org.invalid> wrote:
>>
>>> On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
>>>> Wojtek writes:
>>>>
>>>>> You check for the number of "slots" in the array:
>>>>>
>>>>> if ( index>= 0&& index< part.length )
>>>>> do something with part[index]
>>>>> else
>>>>> error();
>>>>
>>>> Shame that (0<= index< part.length) means something useless instead.
>>>
>>> Be thankful that its meaning in Java is more useful than in C.
>>
>> Please explain that. I'm probably wrong, but I'd think they'd be the
>> same.
>>
>> Evaluate one of the comparisons (not positive which one), resulting in
>> a boolean. Convert the boolean to a number to do the other comparison.
>> Not very useful.
>>
>
> I'd wager that refusing to compile is more useful than meaning something
> useless and confusing, therefore errors-prone.

     Bingo.  (That at least is what I meant; whether it's "true" is
another matter entirely.)

     In C, `x < y < z' has a well-defined meaning, but one that is so
very unlikely to be intended that it is almost surely a mistake.  The
meaning is: "Compare x to y, yielding 1 if x is in fact less than y
or 0 if not. Then compare that 1 or 0 to z, yielding 1 if it is less
than z or 0 if it is not."

     If z is not in (0,1] the expression's value is foreordained as
0 (for z <= 0 or z a NaN) or 1 (for z > 1).  An expression with a
foreordained value is, I think, deserving of being called "useless."

     If z is in (0,1] The expression `x < y < z' is equivalent to
`!(x < y)', which is in turn equivalent to `x >= y' if NaN's are
not involved.  Either of the latter two forms is, I'd say, greatly
preferable, and the original is "useless" because it would be better
written in one of the other ways.

     In Java `x < y < z' is a compile-time error.

     That's why I think Java's interpretation is superior.  Not that
Java gets *everything* right: If x,y,z are booleans `x == y == z'
is valid and means something other than what might be expected.
Still, it's an improvement on C.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#9269

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2011-10-28 10:08 +0300
Message-ID<qotvcr9ioev.fsf@ruuvi.it.helsinki.fi>
In reply to#9267
Eric Sosman writes:
> On 10/27/2011 9:45 AM, Mayeul wrote:
> > On 27/10/2011 15:09, Tim Slattery wrote:
> >> Eric Sosman wrote:
> >>
> >>> On 10/27/2011 3:20 AM, Jussi Piitulainen wrote:
> >>>> Shame that (0<= index< part.length) means something useless instead.
> >>>
> >>> Be thankful that its meaning in Java is more useful than in C.
> >>
> >> Please explain that. I'm probably wrong, but I'd think they'd be the
> >> same.
> >>
> >> Evaluate one of the comparisons (not positive which one), resulting in
> >> a boolean. Convert the boolean to a number to do the other comparison.
> >> Not very useful.
> >
> > I'd wager that refusing to compile is more useful than meaning something
> > useless and confusing, therefore errors-prone.
> 
>      Bingo.  (That at least is what I meant; whether it's "true" is
> another matter entirely.)
> 
>      In C, `x < y < z' has a well-defined meaning, but one that is so
> very unlikely to be intended that it is almost surely a mistake.  The
...
>      In Java `x < y < z' is a compile-time error.
> 
>      That's why I think Java's interpretation is superior.  Not that
> Java gets *everything* right: If x,y,z are booleans `x == y == z'
> is valid and means something other than what might be expected.
> Still, it's an improvement on C.

Java does not get this right. It's merely a mild improvement on C.
Python gets this right: 0 <= k < n means what one expects.

Comparison chains are not hard to understand or implement: just add a
syntactic category of relation symbols (<, >, <=, >=, ==, !=) with the
same low precedence, associative, and give the resulting expression
the intended meaning that everybody learns to expect in school.

Since the current meaning of chained comparisons in Java is not useful
for much anything, the change would not be impossible. This does not
mean that I expect it to happen. I do not expect it to happen.

The languages that get chained comparisons right have some bragging
rights over Lisp. While Lisp does 0 < k < n with ease, it gets uneasy
with the different comparisons in 0 <= k < n, which often is what one
wants to test.

[toc] | [prev] | [next] | [standalone]


#9272

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-10-28 06:20 -0700
Message-ID<ptala7t3vv8gl8hnd8af2sd0hi195hop3n@4ax.com>
In reply to#9175
On 25 Oct 2011 16:26:40 GMT, jochen2@brenz.com (Jochen Brenzlinger)
wrote, quoted or indirectly quoted someone who said :

>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")) {
>   ....
>   }
>
>So what else can I use?

A computer language is much pickier than a human language.  You can't
just try syntax out.There are billions of plausible ways that don't
work.

You have to copy something you have seen known to work.  You need a
textbook. see http://mindprod.com/jgloss/gettingstarted.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to 
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.

[toc] | [prev] | [next] | [standalone]


#9663

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-06 14:54 -0500
Message-ID<4eb6e5e6$0$294$14726298@news.sunsite.dk>
In reply to#9175
On 10/25/2011 12:26 PM, 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")) {
>     ....
>     }
>
> So what else can I use?

if(part.length > 4 &&  part[4].equals("hello")) {

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web