0

I have read so many articles but still don't get it.

I have a serial data which is a String type and i want to compare it with an integer.

This is the read serial data code:

case MESSAGE_READ:
                int i;

                byte[] readBuf = (byte[]) msg.obj; 


             // construct a string from the valid bytes in the buffer
                String readMessage = new String(readBuf, 0, msg.arg1);
                Log.i(LOG_TAG, readMessage);

                mTextView.setText(readMessage);
                String [] numbers = readMessage.split("\n");
                int [] intNumber = new int[numbers.length];
                //List<Integer> integers = new ArrayList<Integer>();


                for (String number : numbers) {

                    //integers.add(Integer.valueOf(number.trim()));
                    for (i=0;i<numbers.length;i++){
                        intNumber[i] = Integer.valueOf(number.trim());
                        if (intNumber[i]<0 || intNumber[i]>95){
                            //Some Actions
                        }
                    }
                }
                break;

And the LogCat shows:

05-06 17:28:06.919: D/BluetoothReadService(15090): connected
05-06 17:28:06.919: D/BluetoothReadService(15090): create ConnectedThread
05-06 17:28:06.929: D/BluetoothReadService(15090): setState() 2 -> 3
05-06 17:28:06.949: I/BluetoothReadService(15090): BEGIN mConnectedThread
05-06 17:28:06.969: I/HBAS(15090): MESSAGE_STATE_CHANGE: 3
05-06 17:28:07.079: I/HBAS(15090): 24

05-06 17:28:07.079: I/HBAS(15090): 25

05-06 17:28:07.079: I/HBAS(15090): 26

05-06 17:28:07.079: I/HBAS(15090): 27

05-06 17:28:07.079: I/HBAS(15090): 28

05-06 17:28:07.079: I/HBAS(15090): 29

05-06 17:28:07.079: I/HBAS(15090): 0

05-06 17:28:07.079: I/HBAS(15090): 1

05-06 17:28:07.079: I/HBAS(15090): 2

05-06 17:28:07.079: I/HBAS(15090): 3

05-06 17:28:07.079: I/HBAS(15090): 4

05-06 17:28:07.079: I/HBAS(15090): 5

05-06 17:28:07.079: I/HBAS(15090): 6

05-06 17:28:07.079: I/HBAS(15090): 7

05-06 17:28:07.079: I/HBAS(15090): 8

05-06 17:28:07.079: I/HBAS(15090): 9

05-06 17:28:07.079: I/HBAS(15090): 10

05-06 17:28:07.079: I/HBAS(15090): 11

05-06 17:28:07.079: I/HBAS(15090): 12

05-06 17:28:07.079: I/HBAS(15090): 13

05-06 17:28:07.079: I/HBAS(15090): 14

05-06 17:28:07.079: I/HBAS(15090): 15

05-06 17:28:07.079: I/HBAS(15090): 16

05-06 17:28:07.079: I/HBAS(15090): 17

05-06 17:28:07.079: I/HBAS(15090): 18

05-06 17:28:07.079: I/HBAS(15090): 19

05-06 17:28:07.079: I/HBAS(15090): 20

05-06 17:28:07.079: I/HBAS(15090): 21

05-06 17:28:07.079: I/HBAS(15090): 22

05-06 17:28:07.079: I/HBAS(15090): 23

05-06 17:28:07.079: I/HBAS(15090): 24

05-06 17:28:07.079: I/HBAS(15090): 25

05-06 17:28:07.079: I/HBAS(15090): 26

05-06 17:28:07.079: I/HBAS(15090): 27

05-06 17:28:07.079: I/HBAS(15090): 28

05-06 17:28:07.079: I/HBAS(15090): 29

05-06 17:28:07.079: I/HBAS(15090): 0

05-06 17:28:07.079: I/HBAS(15090): 1

05-06 17:28:07.079: I/HBAS(15090): 2

05-06 17:28:07.079: I/HBAS(15090): 3

05-06 17:28:07.079: I/HBAS(15090): 4

05-06 17:28:07.079: I/HBAS(15090): 5

05-06 17:28:07.079: I/HBAS(15090): 6

05-06 17:28:07.079: I/HBAS(15090): 7

05-06 17:28:07.079: I/HBAS(15090): 8

05-06 17:28:07.079: I/HBAS(15090): 9

05-06 17:28:07.079: I/HBAS(15090): 10

05-06 17:28:07.079: I/HBAS(15090): 11

05-06 17:28:07.079: I/HBAS(15090): 12

05-06 17:28:07.079: I/HBAS(15090): 13

05-06 17:28:07.079: I/HBAS(15090): 14

05-06 17:28:07.079: I/HBAS(15090): 15

05-06 17:28:07.079: I/HBAS(15090): 16

05-06 17:28:07.079: I/HBAS(15090): 17

05-06 17:28:07.079: I/HBAS(15090): 18

05-06 17:28:07.079: I/HBAS(15090): 19

05-06 17:28:07.079: I/HBAS(15090): 20

05-06 17:28:07.079: I/HBAS(15090): 21

05-06 17:28:07.079: I/HBAS(15090): 22

05-06 17:28:07.079: I/HBAS(15090): 23

05-06 17:28:07.079: I/HBAS(15090): 24

05-06 17:28:07.079: I/HBAS(15090): 25

05-06 17:28:07.079: I/HBAS(15090): 26

05-06 17:28:07.079: I/HBAS(15090): 27

05-06 17:28:07.079: I/HBAS(15090): 28

05-06 17:28:07.079: I/HBAS(15090): 29

05-06 17:28:07.079: I/HBAS(15090): 0

05-06 17:28:07.079: I/HBAS(15090): 1

05-06 17:28:07.079: I/HBAS(15090): 2

05-06 17:28:07.079: I/HBAS(15090): 3

05-06 17:28:07.079: I/HBAS(15090): 4

05-06 17:28:07.079: I/HBAS(15090): 5

05-06 17:28:07.079: I/HBAS(15090): 6

05-06 17:28:07.079: I/HBAS(15090): 7

05-06 17:28:07.079: I/HBAS(15090): 8

05-06 17:28:07.079: I/HBAS(15090): 9

05-06 17:28:07.079: I/HBAS(15090): 10

05-06 17:28:07.079: I/HBAS(15090): 11

05-06 17:28:07.079: I/HBAS(15090): 12

05-06 17:28:07.079: I/HBAS(15090): 13

05-06 17:28:07.079: I/HBAS(15090): 14

05-06 17:28:07.079: I/HBAS(15090): 15

05-06 17:28:07.079: I/HBAS(15090): 16

05-06 17:28:07.079: I/HBAS(15090): 17

05-06 17:28:07.079: I/HBAS(15090): 18

05-06 17:28:07.079: I/HBAS(15090): 19

05-06 17:28:07.079: I/HBAS(15090): 20

05-06 17:28:07.079: I/HBAS(15090): 21

05-06 17:28:07.079: I/HBAS(15090): 22

05-06 17:28:07.079: I/HBAS(15090): 23

05-06 17:28:07.079: I/HBAS(15090): 24

05-06 17:28:07.079: I/HBAS(15090): 25

05-06 17:28:07.079: I/HBAS(15090): 26

05-06 17:28:07.079: I/HBAS(15090): 27

05-06 17:28:07.079: I/HBAS(15090): 28

05-06 17:28:07.079: I/HBAS(15090): 29

05-06 17:28:07.079: I/HBAS(15090): 0

05-06 17:28:07.079: I/HBAS(15090): 1

05-06 17:28:07.079: I/HBAS(15090): 2

05-06 17:28:07.079: I/HBAS(15090): 3

05-06 17:28:07.079: I/HBAS(15090): 4

05-06 17:28:07.079: I/HBAS(15090): 5

05-06 17:28:07.079: I/HBAS(15090): 6

05-06 17:28:07.079: I/HBAS(15090): 7

05-06 17:28:07.079: I/HBAS(15090): 8

05-06 17:28:07.079: I/HBAS(15090): 9

05-06 17:28:07.079: I/HBAS(15090): 10

05-06 17:28:07.079: I/HBAS(15090): 11

05-06 17:28:07.079: I/HBAS(15090): 12

05-06 17:28:07.969: I/HBAS(15090): 1
05-06 17:28:08.029: I/HBAS(15090): 3

05-06 17:28:09.019: I/HBAS(15090): 14

05-06 17:28:09.979: I/HBAS(15090): 1
05-06 17:28:10.029: I/HBAS(15090): 5

05-06 17:28:10.989: I/HBAS(15090): 16

05-06 17:28:12.009: I/HBAS(15090): 17

05-06 17:28:12.999: I/HBAS(15090): 18

05-06 17:28:13.999: I/HBAS(15090): 19

05-06 17:28:14.999: I/HBAS(15090): 20

05-06 17:28:16.009: I/HBAS(15090): 21

05-06 17:28:17.009: I/HBAS(15090): 22

05-06 17:28:18.009: I/HBAS(15090): 23

05-06 17:28:19.019: I/HBAS(15090): 24

05-06 17:28:20.019: I/HBAS(15090): 25

05-06 17:28:21.029: I/HBAS(15090): 26

05-06 17:28:22.029: I/HBAS(15090): 27

05-06 17:28:23.029: I/HBAS(15090): 28

05-06 17:28:24.039: I/HBAS(15090): 29

05-06 17:28:25.039: I/HBAS(15090): 0

05-06 17:28:26.029: I/HBAS(15090): 1
05-06 17:28:26.069: I/HBAS(15090): 

05-06 17:28:26.069: D/AndroidRuntime(15090): Shutting down VM
05-06 17:28:26.069: W/dalvikvm(15090): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-06 17:28:26.079: E/AndroidRuntime(15090): FATAL EXCEPTION: main
05-06 17:28:26.079: E/AndroidRuntime(15090): java.lang.NumberFormatException: unable to parse '' as integer
05-06 17:28:26.079: E/AndroidRuntime(15090):    at java.lang.Integer.parseInt(Integer.java:362)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at java.lang.Integer.parseInt(Integer.java:332)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at java.lang.Integer.valueOf(Integer.java:506)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at com.android.hbas.FinalSetting$1.handleMessage(FinalSetting.java:362)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at android.os.Looper.loop(Looper.java:130)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at java.lang.reflect.Method.invoke(Method.java:507)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-06 17:28:26.079: E/AndroidRuntime(15090):    at dalvik.system.NativeStart.main(Native Method)

At start it worked but at some time it FC because of ' ' thing. So any solution??? Thanks for the advice..

1
  • You should click the check mark next to his answer to accept it as the one that solved your issue. That gives him credit and let's others know your issue has been sorted so they can skip over it. Commented May 6, 2012 at 13:04

1 Answer 1

2

check your string is ' ' only.

    if(str.trim().length>0){

       try{ 

       int i =Integer.parseInt(str);

       }catch(NumberFormatException ne){
          System.out.println("could not parse :: " +ne);
        }

    }
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.