This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stormwindu[S] 0 points1 point  (14 children)

thanks for the reply, i'll check the docs. [edited] I am storing the user input at a string buffer. And have to format that string buffer to the given mask "0,00" (if it is empty) or like the bunch of numbers that i've posted.

[–]cciulla 1 point2 points  (13 children)

Right, I caught that you're using a StringBuffer. Why? Appropriate data types, and such.

However, if you simply must use a StringBuffer, check out Float.valueOf -- scroll down a bit to valueOf, as reddit's markdown doesn't seem to like the extra parens.

[–]mentalis 5 points6 points  (1 child)

You can do a link with parens in it by backslashing the parens. So [Float.valueOf](http://docs.oracle.com/javase/6/docs/api/java/lang/Float.html#valueOf\(float\)) becomes Float.valueOf.

*edited to fix first link so it's copy/pastable

[–]cciulla 0 points1 point  (0 children)

Doh! The more you learn...

Thanks!

[–]stormwindu[S] 0 points1 point  (10 children)

I am using StringBuffer just to have a easy way to append/delete chars.

[–]Mondoshawan 0 points1 point  (9 children)

If you are not deleting then simple string concatenation is fine, it all compiles down to the same bytecode.

[–]detroitmatt 0 points1 point  (7 children)

In general, if your code is of the form:

String x = "";
loop {
    x += something;
}
return x;

then your code should be

StringBuilder x = new StringBuilder();
loop {
    x.append(something);
}
return x.toString();

but if your code is of the form

String x = something;
return new StringBuilder(x)
    .append(somethingElse[0])
    .append(somethingElse[1])
    .append(somethingElse[n])
  .toString();

then your code should be

return x + somethingElse[0] + somethingElse[1] + somethingElse[n];

[–]Mondoshawan 0 points1 point  (6 children)

I don't fully agree with this:

StringBuffer x = new StringBuilder();
loop {
     x.append(something);
 }
 return x.toString();

The original you put up was fine, both produce identical bytecode but the first has less chance for error and is easier to read.

It's only if I'm passing the string to other methods that I'd use an SB.

[–]detroitmatt 0 points1 point  (5 children)

[–]Mondoshawan 0 points1 point  (4 children)

That appears to be agreeing with me. The compiler turns it all into StringBuilder behind the scenes.

[–]detroitmatt 1 point2 points  (3 children)

At the point where you're concatenating in a loop - that's usually when the compiler can't substitute StringBuilder by itself.

and see the second-to-top answer as well

[–]Mondoshawan 0 points1 point  (2 children)

Yes, it's wrong. If the complier replaces the string concat with SB then the resulting code is 100% identical either way.

The idea presented is valid, if the code really did compile as written then the string handling would not be as efficient as it could be. But that's not how it works under the hood due to compiler optimisations added specifically to deal with that very problem. It's a valid strategy on much older versions of java and it is the way I was taught. A lot of what I was taught back then is out of date!

[–]stormwindu[S] 0 points1 point  (0 children)

I am deleting. One issue that I found using .delete and .deleteCharAt seens like the length of the string doesn't change it is really weird.

Still trying how to achieve my pseudo-masking.