String.format alternative?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

String.format alternative?

Postby Aqualuna » Fri Nov 02, 2012 5:20 pm

I'm trying to format a string, any ideas on how I could do it?
Aqualuna
New User
 
Posts: 13
Joined: Mon Oct 29, 2012 9:52 pm

Re: String.format alternative?

Postby skoehler » Fri Nov 02, 2012 8:54 pm

Aqualuna wrote:I'm trying to format a string, any ideas on how I could do it?


You're trying to format an Integer, Double, Float, or whatever. However, you are not telling me, what you are trying to do, for example by pasting your format String.

Of course, String.format is a beast, some form of swiss knife. By using it, you would pull in several kilobytes of code and dependencies. Unfortunately, leJOS does not have very sophisticated methods of formatting floats and integers, simply because nobody has implemented it.

If you're just trying to assemble some basic String, without the need for any fancy formatting of numbers, use the StringBuilder class.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: String.format alternative?

Postby Aqualuna » Fri Nov 02, 2012 11:51 pm

It wasn't for any specific format, I was coding a form class and wanted to pass a format string to it.
I checked a few Formatter.java implementations, and they are indeed quite fat, all the locale and internationalization adds too much fat.
Eventually I could take an old C implementation to adapt it, but for now I'll just avoid the formatting altogether.

Thank for the help
Aqualuna
New User
 
Posts: 13
Joined: Mon Oct 29, 2012 9:52 pm

Re: String.format alternative?

Postby skoehler » Fri Nov 02, 2012 11:58 pm

In Germany, we have a saying: You're using a cannon to shoot a bird.
Meaning: you're using a tool far too big for the job at hand.

I think, what you're search for is the functor pattern or function objects or something similar. Simply create an interface that defines the method that converts objects to string and back again, and pass it to you form field.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: String.format alternative?

Postby Aqualuna » Sat Nov 03, 2012 12:10 am

Already solved it, I realized most of the times I'll just need "text number" or "text number text" so i just defined a prefix and suffix text and that's it. I'm not even going to mess with number formatting either, with the right numbers I shouldn't get decimal floating point errors (x.000000001 and x.999999999).
Aqualuna
New User
 
Posts: 13
Joined: Mon Oct 29, 2012 9:52 pm

Re: String.format alternative?

Postby skoehler » Sat Nov 03, 2012 12:12 am

Aqualuna wrote:I'm not even going to mess with number formatting either, with the right numbers I shouldn't get decimal floating point errors (x.000000001 and x.999999999).

Or use Math.round()
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: String.format alternative?

Postby Aqualuna » Sat Nov 03, 2012 12:40 am

Yeah in the end I'm passing a parameter to specify number of digits, trying to finish this part quick because its kinda boring already.

I just built my first robotic car (kinda of a dragster) to test how fast it could run, so I'm coding a simple interface to set the power and stop distance (ultrasonic sensor). I want to log the distance and time during the "drag race" (gonna try the logging functions there) and then Ill compute the speed and acceleration curves in matlab.
Aqualuna
New User
 
Posts: 13
Joined: Mon Oct 29, 2012 9:52 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff