0

I have this Runnable

Runnable serverChecksRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            if (connectedSuccess == true)
            {
                    checkServer = Get(iptouse + "uploadstatus");

            }

            Handler h=new Handler(Looper.getMainLooper());
            h.post(new Runnable()
            {
                @Override
                public void run()
                {
                    if (connectedSuccess)
                    {
                        if (checkServer != null)
                        {
                            String a = null;
                            try
                            {
                                a = new String(checkServer, "UTF-8");
                                textforthespeacch = a;
                                {
                                    String varr = textforthespeacch.substring(17,3);
                                    String varr1 = textforthespeacch.substring(0, 16);
                                    String varr2 = textforthespeacch.substring(21);
                                    textforthespeacch = varr1;
                                    status1.setText("Upload completed" + " " + varr + "%");
                                    timerValue.setText("00:00:" + varr2);
                                    numberofuploadedfilescounter += 1;
                                    uploadedfilescount.setText(("Uploaded Files: " + numberofuploadedfilescounter));
                                    MainActivity.this.initTTS();
                                }
                                if (textforthespeacch.contains("uploading"))
                                {
                                    String[] split = textforthespeacch.split(" ");
                                    textforthespeacch = split[0];
                                    status1.setText("Uploading" + " " + split[1] + "%");
                                    servercheckCounter += 1;
                                    if (servercheckCounter == 1)
                                    {
                                        MainActivity.this.initTTS();
                                    }
                                }
                            } catch (UnsupportedEncodingException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            });

            customHandler.postDelayed(serverChecksRunnable,1000);
        }
    };

The var textforthespeacch is string and get text from a web server. Then i check when it contains a part of a text assign the text parts to three vars:

if (textforthespeacch.contains("upload completed"))
                                {
                                    String varr = textforthespeacch.substring(17,3);
                                    String varr1 = textforthespeacch.substring(0, 16);
                                    String varr2 = textforthespeacch.substring(21);

The problem is that the program crash on the first substring(17,3)

The text in the textforspeech is: "upload completed 100 0" The number 100 present percentages and will be all the time 100. And the number 0 present seconds it can be changed sometimes 0 sometimes 1 and sometimes 34.5

In varr I need to put the 100 as string "100" In varr1 "upload completed" in varr2 "0"

varr2 might be changed to can be "0" or "1" or "45.5" the others varr and varr1 never change the text "100" and "upload completed" never change.

This is the exception message in the logcat i'm getting:

09-15 18:45:45.435    5583-5583/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.webservertest, PID: 5583
    java.lang.StringIndexOutOfBoundsException: length=22; regionStart=17; regionLength=-15
            at java.lang.String.startEndAndLength(String.java:504)
            at java.lang.String.substring(String.java:1333)
            at com.adilipman.webservertest.MainActivity$2$1.run(MainActivity.java:235)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
6
  • 4
    textforthespeacch.substring(17,3);. This should be textforthespeacch.substring(3, 17); Commented Sep 15, 2015 at 15:47
  • 2
    hay Daniel, Post only relevant part of code, it is always easy to understand & attractive too Commented Sep 15, 2015 at 15:48
  • Off-topic but: if (connectedSuccess == true) is invitation to problems like if (connectedSuccess = true) (= is assignment, not comparison) so you should avoid this style. It is better to use if (connectedSuccess) (or if (!connectedSuccess) instead of ==false). You can also try yoda-style if (true==connectedSuccess), it is safe since it is impossible to assign any value to true literal. Commented Sep 15, 2015 at 15:52
  • Emd4600 no. The string is: "upload completed 100 0" when i did in varr1 0,16 i got the "upload completed" part and this is fine. Now in varr i need to get only the "100" and in varr2 i need to get only the "0" and the "0" might be next time "1" or "45.5". Doing 3,17 in varr give me "oad completed" and i need in varr "100" Commented Sep 15, 2015 at 16:01
  • 1
    Then what you want to do is string.substring(17, string-length()-2); (note it might be - 3, I'm not sure) Commented Sep 15, 2015 at 16:05

2 Answers 2

1

With this line

String varr = textforthespeacch.substring(17,2);

The first value in a substring call must be less than the second. That is why you get regionLength=-15.

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

Comments

1

Here is the syntax for the substring() method:

public String substring(int beginIndex)

or

public String substring(int beginIndex, int endIndex)

where

beginIndex -- the begin index, inclusive.

endIndex -- the end index, exclusive.

How can the endIndex in: String varr = textforthespeacch.substring(17,3); be less than the beginIndex.

I think it should be the other way round.

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.