Site icon NgDeveloper

Train Route Java Program

Now you can get the train route information by simply inputting train number to the below program,

Requirement:
Jsoup jar

Train Route Java Program:

[java]
package in.javadomain;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class TrainRoute {

public static void main(String[] args) {
TrainRoute tr = new TrainRoute();
System.out.println(tr.getTrainSchedule("12635"));

}

private String getTrainSchedule(String trainNumber) {
String trainRoute = null;
String trainUrl = "http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi";
try {
Document trainRouteDetails = Jsoup.connect(trainUrl).timeout(0)
.data("lccp_trnname", trainNumber).post();
String parsedTrainNumber = "Train No:"
+ trainRouteDetails.select("table.table_border_both")
.select("tbody").select("tr").get(1).select("td")
.get(0).text() + "";
String trainName = "Train Name:"
+ trainRouteDetails.select("table.table_border_both")
.select("tbody").select("tr").get(1).select("td")
.get(1).text() + "";
String Source = "Source:"
+ trainRouteDetails.select("table.table_border_both")
.select("tbody").select("tr").get(1).select("td")
.get(2).text() + "";
int numberOfPlacesForStopping = trainRouteDetails
.select("table.table_border_both").select("tbody").get(1)
.select("tr").size() – 1;
// System.out.println("Total Stops "+numberOfPlacesForStopping);
String totalStops = "Total Stops " + numberOfPlacesForStopping;
int k = 0;
int numberOfColumns = trainRouteDetails
.select("table.table_border_both").select("tbody").get(1)
.select("tr").select("td").size();
String appendPlaceAndTime = null;
for (int i = 0; i < numberOfColumns; i = i + 9) {
int j = 2;
k++;
if (k <= numberOfPlacesForStopping – 1) {
String stoppingPlace1 = trainRouteDetails
.select("table.table_border_both").select("tbody")
.get(1).select("tr").select("td").get(i + j).text();
String stoppingPlaces1_arraivalTime = trainRouteDetails
.select("table.table_border_both").select("tbody")
.get(1).select("tr").select("td").get(i + 4).text();
// i=i+8;
if (appendPlaceAndTime != null) {
appendPlaceAndTime = appendPlaceAndTime + "\n"
+ stoppingPlace1 + "("
+ stoppingPlaces1_arraivalTime + ")";
} else {
appendPlaceAndTime = stoppingPlace1 + "("
+ stoppingPlaces1_arraivalTime + ")";
}
} else {
String stoppingPlace1 = trainRouteDetails
.select("table.table_border_both").select("tbody")
.get(1).select("tr").select("td").get(i + j).text();
String stoppingPlaces1_arraivalTime = trainRouteDetails
.select("table.table_border_both").select("tbody")
.get(1).select("tr").select("td").get(i + 4).text();
appendPlaceAndTime = appendPlaceAndTime + "\n"
+ stoppingPlace1 + "("
+ stoppingPlaces1_arraivalTime + ")";
}
}

trainRoute = parsedTrainNumber + "\n" + trainName + "\n" + Source
+ "\n" + totalStops + "\n" + appendPlaceAndTime + "\n"
+ "App By Javadomain.in";
// System.out.println(trainRoute);
} catch (IOException e) {
trainRoute = "Server may be busy or down";
}
return trainRoute;
}

}
[/java]

Output:

[plain gutter=”0″]
Train No:12635
Train Name:VAIGAI EXP
Source:CHENNAI EGMORE
Total Stops 9
CHENNAI EGMORE(Source)
TAMBARAM(13:44)
CHENGALPATTU(14:13)
VILLUPARAM JN(15:45)
VRIDHACHALAM JN(16:28)
ARIYALUR(17:04)
TIRUCHCHIRAPALI(18:30)
DINDIGUL JN(20:03)
MADURAI JN(21:25)
App By Javadomain.in
[/plain]

Revert me back if you need further assistance.

Exit mobile version