Convert a String to an int/float?

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

Moderator: roger

Convert a String to an int/float?

Postby jon419 » Fri Feb 16, 2007 12:26 am

Is there anyway in the lejos subset of Java to convert a string to an int or a float? I can't seem to find a correct way of doing this.


Edit:
Well, I guess I don't see one, so I wrote this:

Code: Select all
int value = 0;
      
for(int i=0;i<array.length;i++) {
    value += ((array[i] - 48) * (Math.pow(10, array.length - i)));
}
      
return value / 10;


I am going to run out of memory quick with this program. Oh well.
jon419
New User
 
Posts: 2
Joined: Thu Feb 08, 2007 12:00 am
Location: Holland, MI

Postby theinevitable » Thu Mar 08, 2007 9:07 pm

You could speed up your code if you did this:

Code: Select all
int value = 0;
       
for(int i=0;i<array.length;i++) {
    value *= 10;  //shift number
    value += ((array[i] - 48); //add digit
}
       
return value;


This way you program will use the multiplication more sparingly which is computationally more expensive.
I dont think a predefined function could perform any faster than this.
theinevitable
New User
 
Posts: 6
Joined: Tue Feb 20, 2007 7:35 pm
Location: London


Return to RCX Software

Who is online

Users browsing this forum: No registered users and 0 guests

cron
more stuff