OutOfMemory exception
Hello!
Any idea why I would get a memory exception with this code.
[code]
import stamp.core.*;
import stamp.peripheral.sensor.compass.HM55B.*; // Compass
import stamp.peripheral.sensor.humidity.sht11.*;
import stamp.peripheral.gps.*; // GPS
/**
* Class for the J-Slave processor of the R2B2 robot.
* Author: P
Any idea why I would get a memory exception with this code.
[code]
import stamp.core.*;
import stamp.peripheral.sensor.compass.HM55B.*; // Compass
import stamp.peripheral.sensor.humidity.sht11.*;
import stamp.peripheral.gps.*; // GPS
/**
* Class for the J-Slave processor of the R2B2 robot.
* Author: P
Comments
str·=·Integer.toString(inData);
Integer.toString() creates a temporary string that is used
only once. After a while you will run out of memory.
Use the Format class.
static char[noparse]/noparse padStr = new char[noparse][[/noparse]7]; //can hold "-32768",0
int value;
Format.sprintf(padStr,"%05d",value);
will create a null terminated decimal string with leading zeroes.
For value is 12, "00012" will be created in padStr.
regards peter
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
Which formatter do I use to get a 3-digit value. If I have "3" I want "003"?
Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
which contains
· /**
·· * Transmits a String. This method blocks until the string is
·· * completely written to the transmit buffer.
·· *
·· * @param data the string to transmit.
·· */
· public void sendString(char[noparse]/noparse data) {
··· int c,i;
··· i = 0;
··· while ((c = data[noparse][[/noparse]i++]) != 0) sendByte(c);
· }
You can add this to your Uart.java file (make sure its ReadOnly attribute is off).
To convert 3 to "003" use
Format.sprintf(padStr,"%03d",value);
which converts value to a 3-digit string with leading zeroes.
But note that value must be in the range 0-999,
otherwise the result is undefined.
regards peter
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy