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

J

Jochen Brenzlinger

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
 
T

Tom McGlynn

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
 
W

Wojtek

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();
 
J

Jussi Piitulainen

Wojtek said:
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.
 
E

Eric Sosman

Wojtek said:
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.
 
T

Tim Slattery

Eric Sosman said:
Wojtek said:
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.
 
M

Mayeul

Eric Sosman said:
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.
 
J

Jussi Piitulainen

Mayeul said:
Eric said:
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.
 
G

Gene Wirchenko

Eric Sosman said:
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
 
T

Tassilo Horn

Gene Wirchenko said:
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
 
E

Eric Sosman

Eric Sosman said:
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.
 
J

Jussi Piitulainen

Eric said:
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.
 
R

Roedy Green

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.
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top