0

I've been googling all night for this problem since it is my first time to use arduino and android. My question is, how can I convert the variable data *String to int? I've been getting NumberFormatException whenever I do: int pulse = Integer.ParseInt(data);

My objective here is to be able to get the data coming from the arduino and have it as an integer, for me to be able to compare it.

ADDITIONAL: The variable "data" is a pulse rate. I need to have it converted to int so I can compare the value if the pulse rate is still normal or not. After hours of searching I found out that what I am trying to convert to int is not purely a string since it came from the arduino, now my problem is how can I make the variable "data" an integer.

This is my code:

public void run()
        {                
            while(!Thread.currentThread().isInterrupted() && !stopWorker)
            {
                try 
                {
                    final int bytesAvailable = mmInputStream.available();                        
                    if(bytesAvailable > 0)
                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.read(packetBytes);
                        for(i=0;i<bytesAvailable;i++)
                        {
                            byte b = packetBytes[i];
                            if(b == delimiter)
                            {
                                final byte[] encodedBytes = new byte[readBufferPosition];
                                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                final String data = new String(encodedBytes, "US-ASCII");
                                readBufferPosition = 0;

                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {   

                                        Intent i = new Intent(Bluetooth.this, Home.class);
                                        i.putExtra("theBPM",data);
                                        startActivity(i);
                                    }
                                });
                            }
                            else
                            {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }
                } 
                catch (IOException ex) 
                {
                    stopWorker = true;
                }
            }
        }
    });

    workerThread.start();

Thank you.

11
  • Well if you ran parseInt on the String and got that exception, then your String is not purely a number. So how can you expect to convert it to a numeric representation? Commented Feb 27, 2015 at 16:35
  • What is your string? Commented Feb 27, 2015 at 16:36
  • Problem is your input data, it is not a int representaiton, Commented Feb 27, 2015 at 16:36
  • @Kon Yeah. I know. But I dont have a clue on how to get it right. Commented Feb 27, 2015 at 16:38
  • @JClassic the "data" variable. Commented Feb 27, 2015 at 16:38

2 Answers 2

1

We assume that the problem is String to int and you already stepped in debug mode or make some Toast message that you are getting the string representation as expected.

You can try the following option:

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

Comments

0

Try printing/logging/making a toast to see what is in data. This can happen only when data has characters other than digits or may be you have a double(decimal) value in data. For that you might need to use Double.parseDouble(data).

2 Comments

Woah. That was it! How'd you know that my variable might not be really an integer?
I tried all possible cases to generate NumberFormatException. Integer.parseInt(data) even works for negative integers :) Good luck.

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.