Question
Hello All, My code is giving me an OutOfBoundException runtime error, kindly help out import java.io.*; import java.util.*; public class Hospital { static ArrayList Vertex
Hello All,
My code is giving me an OutOfBoundException runtime error, kindly help out
import java.io.*;
import java.util.*;
public class Hospital
{
static ArrayList
static ArrayList
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(System.in);
// read in vertices
File file = new File("Vertex.txt");
Scanner infile = new Scanner(file);
String Input = "";
while (infile.hasNextLine())
{
Input = infile.nextLine();
Vertex.add(new Room(Input));
}
//System.out.println(Vertex.size() + " vertices read from file");
// read in edges
file = new File("edge.txt");
infile = new Scanner(file);
String From, Direction, To;
int Count=0;
while (infile.hasNext())
{
Count++;
From = infile.next();
Direction = infile.next();
To = infile.next();
// locate From Vertex in ArrayList
int IndexFrom = 0;
while (!Vertex.get(IndexFrom).RoomName.equals(From))
{ IndexFrom++; }
// locate To Vertex in ArrayList
int IndexTo = 0;
while (!Vertex.get(IndexTo).RoomName.equals(To))
{ IndexTo++; }
// create edge
if (Direction.equals("North"))
{
Vertex.get(IndexFrom).North = Vertex.get(IndexTo);
Vertex.get(IndexTo).South = Vertex.get(IndexFrom);
}
if (Direction.equals("South"))
{
Vertex.get(IndexFrom).South = Vertex.get(IndexTo);
Vertex.get(IndexTo).North = Vertex.get(IndexFrom);
}
if (Direction.equals("East"))
{
Vertex.get(IndexFrom).East = Vertex.get(IndexTo);
Vertex.get(IndexTo).West = Vertex.get(IndexFrom);
}
if (Direction.equals("West"))
{
Vertex.get(IndexFrom).West = Vertex.get(IndexTo);
Vertex.get(IndexTo).East = Vertex.get(IndexFrom);
}
}
//System.out.println(Count + " edges read from file");
System.out.println("Press
//Choice = in.nextLine();
//System.out.println(Vertex.get(0));
Room Temp = Vertex.get(0);
String Choice = " ";
while (!Choice.equals("q"))
{
System.out.println("You are in room " + Temp.RoomName);
System.out.println("Which direction (n, s, e, w) do you want to go? ");
Choice = in.next();
if (Choice.equals ("s") && Temp.South != null)
Temp = Temp.South;
else if (Choice.equals ("s") && Temp.South == null)
System.out.println("You can't go that way!");
else if (Choice.equals ("n") && Temp.North != null)
Temp = Temp.North;
else if (Choice.equals ("n") && Temp.North == null)
System.out.println("You can't go that way!");
else if (Choice.equals ("e") && Temp.East != null)
Temp = Temp.East;
else if (Choice.equals ("e") && Temp.East == null)
System.out.println("You can't go that way!");
else if (Choice.equals ("w") && Temp.West != null)
Temp = Temp.West;
else if (Choice.equals ("w") && Temp.West == null)
System.out.println("You can't go that way!");
}
System.out.println(Temp.South.North.East.West.RoomName);
}
}
class Room
{
String RoomName;
Room North, South, East, West;
boolean Visited; // used for Dijkstra
int Distance; // used for Dijkstra
Room (String theRoomName)
{ RoomName = theRoomName; }
}
here is the Vertex.txt
Entrance SecurityPost Parking Emergency Mortuary WardOne AdminBlock WardTwo Surgery Cafeteria Laboratory BloodBank WardThree
Here is the edge.txt
Entrance East Parking Entrance West SecurityPost Entrance South AdminBlock Cafeteria North AdminBlock Cafeteria West Surgery Cafeteria East Laboratory Cafeteria South WardThree SecurityPost East Entrance WardOne West Mortuary WardOne East AdminBlock WardOne South Surgery Parking West Entrance Parking East Emergency Parking South WardTwo Mortuary East WardOne AdminBlock North Entrance AdminBlock South Cafeteria AdminBlock West Surgery AdminBlock South WardThree Emergency West Parking Surgery North WardOne Surgery East Cafeteria Surgery South BloodBank BloodBank North Surgery BloodBank East WaedThree WardThree North Cafeteria WardThree West BloodBank WardTwo North Parking WardTwo West AdminBlock WardTwo South Laboratory Laboratory North WardTwo Laboratory West Cafeteria
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started