• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What does this if statement check?

 
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this if



does it mean:

if index 0 and 1 is not a space
or the last character before the end of the string is not a space?
 
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wayne,

The equals() method compares two Strings: a literal containing a space (as the first and only character) and
- the first character of a String object called str (in the first part of the boolean expression)
- the last character of str (in the second part of the boolean expression)

What does this comparison actually say?

If the first character of str is not a space OR the last character of str is not a space, then... *do something*

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brecht's analysis is correct.

I don't know why one would use code like this. First, I find the condition itself very suspicious. The conditional statement is not executed when the string both starts AND ends with a space. Why?

Secondly, even if this is really what the author wants to do, then it could be done better using str.startsWith(" ") and str.endsWith(" ").
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, to add to the replies:

Have a look at the API of the String class, what the parameters of the 'substring' method mean: Javadoc
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the logical construct IF (NOT x OR NOT y) can be converted to IF (NOT( x AND y)). DeMorgan's Theorem, if memory serves.

Generally, the fewer NOTs in an expression, the fewer knots in understanding it.
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also if the substrings are just one character each, it's probably easier to use chars:

or

Actually, because we have a concise "not equals" operator "!=", but no con concise "not or" operator, I find the first code easier to understand.  But either works.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Also if the substrings are just one character each, it's probably easier to use chars:

or

Actually, because we have a concise "not equals" operator "!=", but no con concise "not or" operator, I find the first code easier to understand.  But either works.


Your two conditions are not the same. The first one, which is the accurate translation, returns true if either the first or the last character is not a space. The second (wrong) one, returns true unless the first or last character is a space. It's equivalent to firstChar != ' ' && lastChar != ' ', which switches the boolean operator.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I forgot to change the || to && for the second one.

!A || !B --> !(A && B)

!A && !B --> !(A || B)
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic