Mtc Bus Route Java program:
Input – Bus Number.
Eg:
Input – 27B.
Java program:
[java]
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class mtcRoutes {
public static void main(String[] args) {
mtcRoutes mr = new mtcRoutes();
String mtcBusDetails;
try {
mtcBusDetails = mr.getMtcRouteInfo("15B");
System.out.println(mtcBusDetails);
} catch (IOException e) {
System.out.println("Server may be down or busy");
}
}
public String getMtcRouteInfo(String busNumber) throws IOException {
String routeInfo = null;
String mtcRouteUrl = "http://www.mtcbus.org/Routes.asp?cboRouteCode="
+ busNumber + "&submit=Search";
Document document = Jsoup.connect(mtcRouteUrl).timeout(0).get();
Elements busBasicInfo = document.select("table").select("table")
.select("table").select("table").select("table")
.select("tr[bgcolor]").get(1).select("td");
String busBasicInfor = null;
for (Element basicInfo : busBasicInfo) {
if (busBasicInfor != null) {
busBasicInfor = busBasicInfor + "," + basicInfo.text();
} else {
busBasicInfor = basicInfo.text();
}
}
String[] splittingBasicInfo = busBasicInfor.split(",");
String busNumberRetrieved = "Bus No:" + splittingBasicInfo[0];
String busStart = "Start:" + splittingBasicInfo[2];
String busEnd = "End:" + splittingBasicInfo[3];
String journeyTime = "Journey Time:" + splittingBasicInfo[4];
int oddSize = document.select("table").select("table").select("table")
.select("table").select("table").select("tr[bgcolor=white]")
.select("td[colSpan=6]").size();
int evenSize = document.select("table").select("table").select("table")
.select("table").select("table").select("tr[bgcolor=#eaeaea]")
.select("td[colSpan=6]").size();
String oddEvenStop = null;
int oddAppend = 1;
int evenAppend = 2;
for (int i = 0; i < oddSize; i++) {
if (oddEvenStop != null) {
oddEvenStop = oddEvenStop
+ "\n"
+ (oddAppend + i)
+ "."
+ document.select("table").select("table")
.select("table").select("table")
.select("table").select("tr[bgcolor=white]")
.select("td[colSpan=6]").get(i).text();
oddEvenStop = oddEvenStop
+ "\n"
+ (evenAppend + i)
+ "."
+ document.select("table").select("table")
.select("table").select("table")
.select("table").select("tr[bgcolor=#eaeaea]")
.select("td[colSpan=6]").get(i).text();
oddAppend++;
evenAppend++;
} else {
oddEvenStop = (oddAppend + i)
+ "."
+ document.select("table").select("table")
.select("table").select("table")
.select("table").select("tr[bgcolor=white]")
.select("td[colSpan=6]").get(i).text();
oddEvenStop = oddEvenStop
+ "\n"
+ (evenAppend + i)
+ "."
+ document.select("table").select("table")
.select("table").select("table")
.select("table").select("tr[bgcolor=#eaeaea]")
.select("td[colSpan=6]").get(i).text();
oddAppend++;
evenAppend++;
}
}
String busStops = "Bus Stops:\n" + oddEvenStop;
int totalStops = oddSize + evenSize;
String totalstopsString = "Total Stops:" + totalStops;
// System.out.println(busStops);
routeInfo = busNumberRetrieved + "\n" + busStart + "\n" + busEnd + "\n"
+ journeyTime + "\n" + totalstopsString + "\n" + busStops
+ "\n" + "App By Javadomain.in";
return routeInfo;
}
}
[/java]
Output:
[plain gutter=”0″]
Bus No:15B
Start:BROADWAY
End:C.M.B.T.
Journey Time:45
Total Stops:8
Bus Stops:
1.BROADWAY
2.CENTRAL
3.DASAPRAKASH
4.TAYLORS ROAD
5.AMINIJIKARAI
6.NADUVANKARAI
7.ARUMBAKKAM
8.C.M.B.T.
App By Javadomain.in
[/plain]