Message Board


Message Board > Programming > toLower() and toUpper() implications :'D

April 9, 2013, 14:14
Zomg
None
641 posts
Shit, I didn't take into account that chars could be in toUpper() state or in toLower() state.

My code which didn't take that into account:

Java code:
List<Character> alphabet = new ArrayList<Character>();
        for (char a = 'A'; a <= 'Z'; a++) {
            alphabet.add(a);
        }

        String type=null;
        for (int i = 0; i < 26; i++) {
            Element letter = new Element("letter");
            letter.setAttribute("number", i+1+"");
            switch(alphabet.get(i)){
                case 'a':
                    type="klinker";
                    break;
                case 'e':
                    type="klinker";
                    break;
                case 'i':
                    type="klinker";
                    break;
                case 'o':
                    type="klinker";
                    break;
                case 'u':
                    type="klinker";
                    break;

                default:
                    type="medeklinker";
                    break;
            }
            letter.setAttribute("type",type);
            letter.addContent(alphabet.get(i).charValue()+"");
            letters.addContent(letter);
        }


Now of course my code set all the types to 'medeklinker' because all of the letters in my alphabet are pure CAPS.. sigh. :P Need to fix this.

It is a reflex a good programmer has, to check with toUpper() and toLower() cases. :)

Oh.. now I see it. My alphabet list was defined as uppercase chars. Yeah, if you take over someone else's code, be sure to doublecheck it. My mistake. Rofl.

[Edited on April 9, 2013 by Zomg]
____________
#
April 15, 2013, 11:27
Dennis
どこかにいる
2092 posts

Quote:
i+1+""


+"" lol wtf? is that some kind of evil toString casting? I'd pass the value as an int and then perform a ".toString()" on the parameter in the setAttribute method. :)

Tip: Don't make a switch with 5 cases that execute exactly the same statement. Make a list with all vowels and use the ".contains" method instead of ".get", or - if you are constrainted to building it like this - at least use a constant (final static?) with the value "klinker". It's more transparant for future developers and makes the code more reusable.

Example: if you want to translate the app to English you need to change all the statements into "vowel".

I know this is just a small demonstration program, but it's best-practice to build robust code when you program in larger environments.

Just my $0.02.


Note that what I say cannot be 100% syntaxially accurate, since I do not program Java.

Also note that I used syntax as an adverb.


Also note that uppers and lowers are often forgotten by professional programmers, and best-practice as well!
____________
Kwakkel
#
November 10, 2013, 00:02
PB
Defender of the faith
630 posts

What's your opinion, would the fix below be quick and easy or quick and dirty?
switch(alphabet.toLower().get(i)){
____________
#
November 10, 2013, 13:39
Zomg
None
641 posts
I would favor efficiency over quick and dirtiness. Perhaps a hybrid of the two would be a good idea.
____________
#
February 17, 2014, 21:53
Dennis
どこかにいる
2092 posts

The best part of this thread is that it started with "shit" xD
____________
Kwakkel
#
February 18, 2014, 22:22
Zomg
None
641 posts
Shit, that's true. :3
____________
#

Message Board > Programming > toLower() and toUpper() implications :'D

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!