• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

if ( ( numbers.substring(1,2) ) != "0" )

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the following statement valid?
It compiles okay but doesn't seem to work in the program.
if ( ( numbers.substring(1,2) ) != "0" )
What is a better way to do this comparison?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When comparing strings, it is safer to use the equals() method and not the == method. Check this post out for reasonings.
http://www.javaranch.com/ubb/Forum19/HTML/000344.html
Bill
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Since you are actually comparing Strings you need to use the equals() method. For example; numbers.substring(1,2).equals("0").

Also, assuming that "numbers" is your input argument, that particular string is going to only check the 2nd position of the string. For example, if numbers is equal to '10' your statement would return true. If numbers was equal to 101 it would return true as well. Is this what you intend the statement to be checking for?
Pat B.
 
Ed Byrnes
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone...yes, I'm checking for zero's in the Say(b) assignment. The program is becoming quite hairy.
 
reply
    Bookmark Topic Watch Topic
  • New Topic