This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

What does this if statement check?

 
Ranch Hand
Posts: 262
1
  • Mark post as helpful
  • send pies
  • 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?
 
Ranch Foreman
Posts: 117
3
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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: 10408
223
  • Mark post as helpful
  • send pies
  • 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(" ").
 
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • 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
 
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Ranch Foreman
Posts: 3298
22
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Ranch Foreman
Posts: 3298
22
  • Mark post as helpful
  • send pies
  • 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)
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!