Minor WTF of my own...



  • This is how I decided to make sure a char was uppercase in java today:

    someMethod(char c){
        char d[];
        d[0]=c;
        String tmpString=new String(d);
        tmpString=tmpString.toUppercase();
        c=tmpString.atChar(0);
        ...snip...
    }


    Obviously, I changed it to:

    c=(c>'a')?(char)((int)c-(int)'a'+(int)'A'):c;
    followed by:

    c=(c<'A' ||  c>'Z')?(char)((int)'A'+26:c;  //The value just after 'Z' meant non-alphanumeric to the code.



  • Character.isUpperCase(char ch)



  • @bugmenot said:

    Character.isUpperCase(char ch)


    Thanks, as you can probably tell, I'm not very experianced with java.



  • Just to explain, the program displayed characters on a 7x7 grid of LEDs one-by-one. I hand-coded the 'font' (an array of int arrays each holding the numbers of the LEDs to be activated for each letter) into the program, but I only had capitals, a blank, and a small number of punctuation symbols.
    The 'character set' was as follows: 0-25='A'-'Z', 26=blank, 27='!' 28='.' 29=','.
    Basically I handled the punctuation as special cases, converted all text to uppercase and subtracted 'A' and set everything else to blank. This was part of that encoding proccess.



  • @mallard said:

    Just to explain, the program displayed characters on a 7x7 grid of LEDs one-by-one. I hand-coded the 'font' (an array of int arrays each holding the numbers of the LEDs to be activated for each letter) into the program, but I only had capitals, a blank, and a small number of punctuation symbols.
    The 'character set' was as follows: 0-25='A'-'Z', 26=blank, 27='!' 28='.' 29=','.
    Basically I handled the punctuation as special cases, converted all text to uppercase and subtracted 'A' and set everything else to blank. This was part of that encoding proccess.


    You realise that you could have used a 5 x 9 grid, gotten away with four fewer LEDs and gotten a working lower case in the bargain, right? (I spent a lot of time bodging stuff like that together when LEDs were expensive and invisible outdoors, memory chips were 1x1K or 4x256 byte DIPs, TTL logic was king, and the MITS ALTAIR 8800 was the hot new project -- if you could afford to waste that much money on something as useless as a home computer.)



  • @bugmenot said:

    Character.isUpperCase(char ch)


    You should use Character.toUpperCase(char ch), you wont have to bother making an if and converting manually



  • @mallard said:


    Obviously, I changed it to:

    c=(c>'a')?(char)((int)c-(int)'a'+(int)'A'):c;
    followed by:

    c=(c<'A' ||  c>'Z')?(char)((int)'A'+26:c;  //The value just after 'Z' meant non-alphanumeric to the code.




      I think you were doing better with your first version - at least the brackets added up!



      There's a coding-style issue here: the logic of your second
    version there is 'If it's outside the range of uppercase letters A-Z
    then uppercase it else leave it as is'.  A better form of logic
    than "If I dont' want to do anything to this then leave it as is" would
    be "If it IS one of the chars I want to process then process it'.



      Unless, of course, you particularly wanted to try and convert numbers and punctuation to uppercase as well... ;)





      cheers,

          DaveK






  • @DaveK said:

    @mallard said:

    Obviously, I changed it to:

    c=(c>'a')?(char)((int)c-(int)'a'+(int)'A'):c;
    followed by:

    c=(c<'A' ||  c>'Z')?(char)((int)'A'+26:c;  //The value just after 'Z' meant non-alphanumeric to the code.




      I think you were doing better with your first version - at least the brackets added up!



      There's a coding-style issue here: the logic of your second
    version there is 'If it's outside the range of uppercase letters A-Z
    then uppercase it else leave it as is'.  A better form of logic
    than "If I dont' want to do anything to this then leave it as is" would
    be "If it IS one of the chars I want to process then process it'.



      Unless, of course, you particularly wanted to try and convert numbers and punctuation to uppercase as well... ;)





      cheers,

          DaveK






    I noticed the brackets after I posted, it's not a direct copy-and-paste from the code. All those casts needed to stop javac from whinging get annoying and unreadable, fast.
    Secondly, the purpose of the second assignment is to set characters outside the 'character set' to 'A'+26 so that when 'A' is subtracted later, they end up as 26, or blank.











  • @mallard said:

    I noticed the brackets after I posted, it's not a direct copy-and-paste from the code. All those casts needed to stop javac from whinging get annoying and unreadable, fast.

    Did you stop and think that maybe the compiler was on to something?


Log in to reply