0

I have modified the BluetoothChat code to receive serial data from a micro-controller to my android phone. When I run the following code snippet it gives java.util.regex.PatternSyntaxException. What exactly is the error? Why is this exception happening?

BluetoothChat.java

// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {

        case MESSAGE_WRITE:

            byte[] writeBuf = (byte[]) msg.obj;
            // construct a string from the buffer
            String writeMessage = new String(writeBuf);
            mConversationArrayAdapter.add(writeMessage);
            break;

        case MESSAGE_READ:

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

           // construct a string from the valid bytes in the buffer

            String readMessage = new String(readBuf, 0, msg.arg1);

            if(readMessage.matches("*"))
            {
               mConversationArrayAdapter.add("Left");
            }
            else

            {mConversationArrayAdapter.add(readMessage);}

 }

Log-cat:

      09-15 13:54:32.070: E/AndroidRuntime(21931): FATAL EXCEPTION: main
      09-15 13:54:32.070: E/AndroidRuntime(21931): java.util.regex.PatternSyntaxException: Syntax         error in regexp
 pattern near index 1:
      09-15 13:54:32.070: E/AndroidRuntime(21931): *
      09-15 13:54:32.070: E/AndroidRuntime(21931):  ^
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.compileImpl(Native Method)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.compile(Pattern.java:400)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.<init>(Pattern.java:383)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.util.regex.Pattern.matches(Pattern.java:413)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.lang.String.matches(String.java:1765) 09-15 13:54:32.070:
 E/AndroidRuntime(21931):   at
 com.example.footforthought.Sixth$2.handleMessage(Sixth.java:289)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at android.os.Handler.dispatchMessage(Handler.java:99)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at android.os.Looper.loop(Looper.java:137)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at   android.app.ActivityThread.main(ActivityThread.java:4517)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.lang.reflect.Method.invokeNative(Native Method)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at java.lang.reflect.Method.invoke(Method.java:511)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at  .android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
      09-15 13:54:32.070: E/AndroidRuntime(21931):  at dalvik.system.NativeStart.main(Native Method)

2 Answers 2

3

matches() uses regular expressions (regex) for matching the input and * is a regex metacharacter, meaning zero or more of the previous token. Since there's no previous token, it's a syntax error.

If you want to match * literally, escape it with backslash:

readMessage.matches("\\*")

or use some of the string-matching functions such as equals() that don't use a regular expression.

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

1 Comment

Sir, is there any modification to be made if I am reading a two digit number? "mConversationArrayAdapter.add(readMessage);" is giving me a two digit number as two separate digits..
1

Here is your problem :

if(readMessage.matches("*")

If you want to match everything, then use ".*.

Also, like laalto says in his answer. If you want to match everything, you have to escape * either by using Pattern.quote() or by simply using two slashes "\\*".

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.