• 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

String to uppercase reassurance

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey people, being quite new to java i wanted to make sure that this is the best way and that there are no possible errors that could occur with this simple bit of code to check if a string is uppercase?



I cant think of any reason it should ever error but i cant help feeling there's something not quite right?

Thanks a lot.

Marcus.

P.S. Sorry if you think its a stupid question.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing stupid about the question, but there are several possible improvements:
  • 1: Don’t say if (...) return true; else return false; Look here for what would be better style.
  • 2: I would iterate the String char by char and use the methods of the Character class on each char.
  • You need to decide whether a String like "CAMPBELL RITCHIE" is upper-case; what about the space. You might want something like letter ⇒ upperCase

    There ain’t no such thing as an implies ⇒ operator in Java™, so you will have to roll your own.The () are not an essential part of the expression, but ?: has such a low precedence that you usually need () to maintain it as a single value in a larger expression.
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Don’t say if (...) return true; else return false;


    A variation on your favourite bête-noir?

    Look here for what would be better style.


    Not sure I agree with them about using the ternary operator; that's a matter of style.

    @Marcus: While there's absolutely nothing wrong with your code as it stands, it is a bit verbose
    return s.toUpperCase().equals(s);
    is a lot more concise.

    FYI: there's a widely used convention that methods that return booleans start with a basic true/false participle such as 'is', 'has' or 'can' (so in your case isUppercase()). It's by no means mandatory, but you'll find that it often reads better.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote: . . . A variation on your favourite bête-noir? . . .

    No, it hasn’t got [][] in

    I meant the bit about return boo; rather than return boo ? x : y;
     
    Marcus Setchell
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the interest and useful information guys i have come across the who ? : ; method before in PHP my stronger point but even in PHP i choose not use it, a bit unsure why maybe I'm just old fashioned and thanks again. Just for clarification as it stands the method will only be used to match a 1 character String anyway so i think it should be fine.

    Marcus.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Are you absolutely sure it will always be a 1‑length String? What about...using the Boolean identity ¬a ∨ b ≡ a ⇒ b
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Marcus Setchell wrote:Just for clarification as it stands the method will only be used to match a 1 character String anyway so i think it should be fine.


    All the more reason to keep it concise then. Smaller usually == faster; and it certainly does in this case.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Are you absolutely sure it will always be a 1‑length String? What about...


    Very good point. Not sure you need the "boolean identity" stuff though.

    Winston
     
    Marcus Setchell
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I didnt realise that it would make it faster and yes im passing data from a map file using - substring(offset, offset+1); inside a loop so it will be but i will make it more concise for performance purposes thanks again this is why i asked .

    Marcus.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Marcus Setchell wrote:I didnt realise that it would make it faster and yes im passing data from a map file using - substring(offset, offset+1); inside a loop so it will be but i will make it more concise for performance purposes thanks again this is why i asked .


    Even better then, why not just use:
    Character.isUpperCase(str.charOf(offset))
    and no method required at all.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The boolean identity is to demonstrate you can use ¬a ∨ b instead of a ⇒ b.
    If you have a String like "1", that has one character, but is it upper-case? If you use the toUpperCase method you will get "1", so you can’t say it’s not. I thought it meant if it’s a letter then it’s upper‑case, and if‑then is another way to express implies.

    Come to think of it, you can get the same with toLowerCase!

    Do you really need substring() for one-character Strings? You might get away with charAt(). What are you getting out of the Map?
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:The boolean identity is to demonstrate you can use ¬a ∨ b instead of a ⇒ b.


    Yeah, I guessed it was something like that.

    It is worth noting that Character.toUpperCase() only returns true if
    "Character.getType(ch) [returns] UPPERCASE_LETTER, or it has contributory property Other_Uppercase as defined by the Unicode Standard."

    I suppose it might be fun to find uppercase "non-letters", but perhaps on a slow day...

    Winston
     
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As you can see, there are lots of ways. One thing you have to watch for is a null value passed in to your method. Here's a variation that checks for that and assumes you only want uppercase letters in the English alphabet:


    Edit: Originally had s.length() > 1, which would fail to catch an empty string passed in. And fixed the constant with all the letters.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:Here's a variation that checks for that and assumes you only want uppercase letters in the English alphabet:


    And yet another:

    Winston
    reply
      Bookmark Topic Watch Topic
    • New Topic