Question: Can someone fix my code to fix this exception? Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 1 at dijkstrapriorityqueue.Dijkstra.main(Dijkstra.java:149) Dijkstra.java package dijkstrapriorityqueue; import java.io.File; import java.io.FileNotFoundException; import
Can someone fix my code to fix this exception?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at dijkstrapriorityqueue.Dijkstra.main(Dijkstra.java:149)
Dijkstra.java
package dijkstrapriorityqueue; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set;
public class Dijkstra {
static HashMap
public void dijkstra(Vertex departure, String arrival) {
Set
Iterator
while (airportIterator.hasNext()) { Vertex vertex = mapvertex.get(airportIterator.next());
vertex.setKnown(false); vertex.setCost(0); vertex.setIsInfinity(true); vertex.setPrevVertex(null); }
departure.setIsInfinity(false);
Vertex vertex = departure;
vertex.setIsInfinity(false);
boolean arrivalFound = false; PriorityQueue
for (;;) {
try { vertex = travvertx.remove(); } catch (NoSuchElementException nsee) { if (vertex == departure) {
} else { break; } }
if (vertex == null) { break; }
LinkedList
Iterator
while (iterator.hasNext()) { AdjacentVertex adjVertex = iterator.next();
int cost = vertex.getCost() + adjVertex.getCost();
if (adjVertex.getVertex().getKnown() == false) { if (adjVertex.getVertex().getIsInfinity()) { adjVertex.getVertex().setCost(cost); adjVertex.getVertex().setPrevVertex(vertex); adjVertex.getVertex().setIsInfinity(false); } else { if (adjVertex.getVertex().getCost() > cost) { adjVertex.getVertex().setCost(cost); adjVertex.getVertex().setPrevVertex(vertex); } }
if (!travvertx.contains(adjVertex.getVertex())) travvertx.add(adjVertex.getVertex()); } }
vertex.setKnown(true); } }
public int printPath(Vertex departure, Vertex arrival) { if (arrival == departure) { route.append(arrival.getName() + " -> ");
return -1; } else { int numberOfConnections = 1 + printPath(departure, arrival.getPrevVertex());
route.append(arrival.getName() + " -> ");
return numberOfConnections; } }
public static void main(String[] args) throws FileNotFoundException { File airports = new File("airports.txt");
Scanner sc1 = new Scanner(airports);
while (sc1.hasNext()) { String airportDetail = sc1.nextLine(); String[] splitString = airportDetail.split(" "); Vertex airport = new Vertex(splitString[0]); mapvertex.put(splitString[0], airport); }
sc1 = new Scanner(airports);
while (sc1.hasNext()) { String airportDetail = sc1.nextLine(); String[] splitString = airportDetail.split(" "); Vertex airport = mapvertex.get(splitString[0]); LinkedList
for (int j = 1; j < splitString.length; j++) { String[] subStrings = splitString[j].split(" "); int cost = Integer.parseInt(subStrings[1]); listadjvertx.add(new AdjacentVertex(mapvertex.get(subStrings[0]), cost)); }
airport.setadj(listadjvertx); }
while (true) { Dijkstra dij = new Dijkstra(); Scanner sc = new Scanner(System.in); System.out.print("Enter Departure Airport: "); String departure = sc.next().toUpperCase(); System.out.print("Enter Arrival Airport: "); String arrival = sc.next().toUpperCase();
Vertex depvertx = mapvertex.get(departure); Vertex arrvertx = mapvertex.get(arrival);
System.out.println(""); dij.dijkstra(depvertx, arrival);
System.out.println("By Price:"); System.out.println("");
int numberOfConnections = dij.printPath(depvertx, arrvertx);
System.out.println("Price : " + arrvertx.getCost()); System.out.println("Connection(s): " + numberOfConnections);
dij.route.toString();
System.out.println("Route : " + dij.route.substring(0, dij.route.length() - 4)); System.out.println(""); System.out.println("Check Another Route? (Y/N)");
if (sc.next().toUpperCase().equals("N")) break; } } }
Vertex.java
package dijkstrapriorityqueue; import java.util.LinkedList;
public class Vertex implements Comparable
public Vertex(String name) { this.name = name; this.cost = 0; this.known = false; this.isInfinity = true; this.prevVertex = null; }
public LinkedList
public void setadj(LinkedList
public String getName() { return this.name; }
public int getCost() { return this.cost; }
public void setCost(int cost) { this.cost = cost; }
public boolean getKnown() { return this.known; }
public void setKnown(boolean known) { this.known = known; }
public boolean getIsInfinity() { return this.isInfinity; }
public void setIsInfinity(boolean isInfinity) { this.isInfinity = isInfinity; }
public Vertex getPrevVertex() { return this.prevVertex; }
public void setPrevVertex(Vertex prevVertex) { this.prevVertex = prevVertex; }
public int compareTo(Vertex vertex) { return this.cost - vertex.getCost(); }
public String toString() { return this.name; } }
AdjacentVertex.java
package dijkstrapriorityqueue;
public class AdjacentVertex { private Vertex vertex; private int cost;
public AdjacentVertex(Vertex vertex, int cost) { this.vertex = vertex; this.cost = cost; }
public Vertex getVertex() { return this.vertex; }
public void setVertex(Vertex vertex) { this.vertex = vertex; }
public int getCost() { return this.cost; }
public void setCost(int cost) { this.cost = cost; } }
airports.txt
ATL BOS 250 DFW 250 MOB 59 AUS DFW 59 HOU 59 SAT 59 BOS ATL 250 DFW 250 DFW ATL 250 AUS 59 BOS 250 HOU 128 LAX 1000 LIT 59 MSY 128 OKC 59 SHV 59 SFO 1200 HOU AUS 59 DFW 128 SAT 59 LAX DFW 1000 SFO 100 LIT DFW 59 MOB ATL 59 MSY DFW 128 OKC DFW 59 SAT AUS 59 HOU 59 SFO DFW 1200 LAX 100 SHV DFW 59
Thanks in advance!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
