• 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

Determing equality of String

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method that returns a String value. The values are only valid if it is 1-9. How do you do determine greater than and less than of a String?
for example: (I know this does not work)
if (myString.getString() > "0" && myString.getString < "10")
{
doSomething;
}
Thanks for any suggestions.
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just convert them to int and do your compare.
 
Bart Wilson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a string because it does not involve calculations. It's a value getting off of a form.
Thanks.

Originally posted by Paul Stevens:
Why not just convert them to int and do your compare.


 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try
int stringInt = Integer.parseInt(myString.getString());
if (stringInt > 0 && stringInt < 10)
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bart, you are doing a calcuation. You are calculating whether the entered number is not less than 1 and not greater than 9.
 
Bart Wilson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike:
The value is a String comprised of two Strings
for example
strClass = strPrimaryClasss + "/" + strSecondaryClass
strClass could equal 9/10 or 9 only or 10 only.
There is a third String that would indicate whether strClass is just a primary, secondary or combination of both classes.
That's why it's a String, no calculations are done on these values.
Thanks.

Originally posted by Mike Curwen:
Bart, you are doing a calcuation. You are calculating whether the entered number is not less than 1 and not greater than 9.


 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, talk about confusing.
Is strPrimaryClass a real class or just a String?
If it is a real class you could put the check for valid range into that class. To check for valid range you would want to do something like Cindy posted. Than if the class made it past that valid range check you would know that it was safe to use the string representation of the int.
If strPrimaryClass is just a String than you would want to do a valid range check, with something like Cindy posted, before you did something like:
"strClass = strPrimaryClasss + "/" + strSecondaryClass"
In either case Cindy and Mark are right. You just need to figure out how to apply the logic.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stand by my original statement.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would argue that *before* you make the one larger string with a /, that you check each of the primary and secondary strings for valid number ranges... by converting them to integers and comparing with the < or > symbols. Like Cindy showed you, and Paul and now I'm suggesting.

It gets pretty messy if you are allowing a user to enter any values, and then have to sort it out later. You originally said you have a method that returns a String value, and we now understand it can be of the form x[/x] where x={1...9} How about that method does the checking? it should have the individual strings for primary and secondary. Otherwise, you'd simply have to split these strings out again, convert them to integers and do your comparison.

Plus if you are getting these "off a form", there are Plenty of pretty good ways to limit user input on web forms. Head it all off at the pass by only allowing the values 1...9 to be entered.

 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn we're good!
Now if Bart would only come back and praise us.
 
Bart Wilson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great job guys! Didn't expect it to be so heavy.
The classes are categories not java classes.
The value of the combination of these two classes is derived. The user does not enter it on the form. The values come from the database.
A 9/10 class is not the same as a 9 class and a 10 class.
A 9/10 class may have some items of 9 class and 10 class and it may have additional stuff that are not in 9 or 10. Confusing? Yes. But I don't question the business rules and policies. Just doing what they want and having them as Strings make more sense.
Great job!
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic