Post by Avtar on Nov 1, 2008 20:02:03 GMT
No seriously. I've got some lab internals coming up, so I was going through a couple of programming concepts and I just noticed something. We have a couple of elementary programs, and there's this one in which the reverse of a string is to be determined during the course of the program. Without using built-in functions, what most people would do, is that if the string is stored as a character array, just traverse it backwards and print it, or traverse it backwards and store it in a new character array. But since Java is the mother of all languages and has built-in methods for everything, you can use the reverse() method.
So, here I am.
string str;
So I've declared a string and got the user to enter a value to it.
System.out.println(str.reverse());
And then I run that, since well, that should work now shouldn't it. But then I'm told I've failed, since the reverse() method is defined in the StringBuffer class.
>.<
So.
StringBuffer s = new StringBuffer(str);
And then...
System.out.println(s.reverse());
Would have been so much simpler if I could just do it straight from the String class. But going through the documentation, I noticed that all manipulations performed on strings are stored in the StringBuffer class. Doesn't make much sense to me, thought it would just be much easier this way. Any other quips you've come across in languages that you wished weren't so?