I'm trying to make an application that will parse the page with the bus schedule and display it.

The bottom line is that the schedule was taken first stop and if the bus is not earlier than the current time. That is, if it is 18:10, then it is necessary to output the remaining schedule from 18:10.

http://mybuses.ru/moscow/bus/734/

Ideally, there would only be a display of 3 buses that are closest to the current time.

class main extends AsyncTask<Void, Void, Void>{ String Bus1,Sys_time,wtf; @Override protected Void doInBackground(Void... params) { String table=""; String time; Integer first,second; final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); Date date = cal.getTime(); Integer mHour = date.getHours(); Integer mMinute = date.getMinutes(); if(mMinute<10){ mMinute=date.getMinutes()+10; } String curr_time=mHour.toString()+":"+mMinute.toString(); Document doc; try { doc = Jsoup.connect("http://mybuses.ru/moscow/bus/734/").get(); Element table_site=doc.select("table").get(0); Element rows1=table_site.select("tr").get(1); Iterator<Element> rowIterator=table_site.select("tr").iterator(); rowIterator.next(); Integer str1,str2,str3,str4; while (rowIterator.hasNext()) { Element e = doc.select("tr").get(2); String e_1=e.attr("class"); str1=e_1.indexOf("-"); str2=e_1.indexOf(":"); str3=e_1.indexOf("-"); str4 = e_1.indexOf(":"); first = Integer.parseInt(e_1.substring(str1+1, e_1.indexOf(str1+2))); second = Integer.parseInt(e_1.substring(str2+1, e_1.indexOf(str2+2))); if (first>=mHour && second>=mMinute) { Element rows=table_site.select("tr").get(1); Element record1=rows.select("td").get(1); Iterator<Element> time1=record1.select("td").iterator(); wtf = time1.next().text(); } else { wtf="1232132132"; } } Element record2=rows1.select("td").get(6); Iterator<Element> time2=record2.select("td").iterator(); table="Автобус прибудет на Ст. Солнечная в "+wtf+" (через n минут)\nАвтобус прибудет на ГМС3 в "+time2.next().text(); } catch (IOException e) { e.printStackTrace(); } Bus1=table; Sys_time=curr_time; return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); textView.setText("Текущее время: "+Sys_time+"\n\nРасписание автобусов: \n"+Bus1); } } 

This code has an error:

Caused by: java.lang.StringIndexOutOfBoundsException: length = 41; regionStart = 23; regionLength = -24

in line

 first = Integer.parseInt(e_1.substring(str1+1, e_1.indexOf(str1+2))); 

How can I fix it? My idea was to get time from the class name on the site, because the class name indicates the time of the first stop of sh2 columnIsVisible 0-05:26-01-0002-0001 , where 05:26 is the time. I tried to cut this time and compare it with the current one.

    1 answer 1

    The exception to you in English says that you are beyond the boundaries of the line.

    And now let's try to parse a fragment of your code.

     str1 = e_1.indexOf("-"); ... ... first = Integer.parseInt(e_1.substring(str1+1, e_1.indexOf(str1+2))); 
    1. in line e_1 is a line for parsing.
    2. integer variable str1 character index -
    3. in the variable first we want to get an integer into which the string is converted from two characters after the character -

    It's all clear. But then the interesting begins.

    e_1.indexOf(str1+2) - here you are trying to find the index of the occurrence of the symbol in the source string, which is the sum of the numbers from the index str1 + 2. Those. actually illogical and incorrect operation. I think that it is quite possible to replace it with just NV str1+3 . 3 - because the final index is not included in the result of the operation.

    Those. the code should be.

     first = Integer.parseInt(e_1.substring(str1+1, str1+3)); 

    Similarly, to get the variable second

    • Thank you very much, only I figured it out myself. =) - GinTR1k