0

I have a function that receives a number (milliseconds to be precise) via Bluetooth.

byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "UTF-8");
readBufferPosition = 0;

started = "";
int myInt;
if (data != "") {
  try{
    myInt = new Integer(data);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(myInt);
  }catch(NumberFormatException ex){
    System.err.println("NumberFormatException "+ ex.getMessage());
  }
  ...
} else {
  ...
}

The above try throws:

NumberFormatException Invalid int: "8250

I think the data received contains more then just the milliseconds, but how can I find that out, when I do

myLabel.setText(data);

it shows me: 8250

Any ideas?

3
  • 2
    try new Integer(data.trim()); can be a \n Commented May 23, 2015 at 15:43
  • try myInt = Integer.valueOf(data); Commented May 23, 2015 at 15:45
  • @ Luis Felipe Kaufmann da silva perfect, that was it... Commented May 23, 2015 at 15:47

1 Answer 1

1

Your data is a String. When you try to form an Integer later, this fails. You need to use this: myInt = new Integer(Integer.parseInt(data));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.