简体   繁体   中英

Index out of bounds error for java program

I'm getting this error and I need help. Code reads in a.txt file and gives an output. I believe it should be a simple fix but am unable to find a solution. The first token represents the name of a room in the dungeon. After that, each pair of tokens represents a connected room and the steps it will take to travel to that room. (A room can be connected to up to 32 other rooms). If a room doesn't have an entry in the input file, this means that it does not have any way out. Every dungeon entrance is named A. When there are multiple connected rooms, they should be explored in alphabetic order. The room labeled X contains a heart container. Not all rooms are connected. Input:

A B 25 G 11 I 7
B C 10 E 12
C E 1 H 25 L 88
D A 51 J 2 N 48
E A 99 B 23 K 3
F C 23 D 3 E 75
G A 12 D 29 X 9 
H G 1 C 2 M 10
I E 7 B 2 A 9
J I 12 H 11 G 13 F 27 D 85
K A 12 B 13
L E 70 G 50 J 20 K 1 B 4
M L 10 A 11 F 91 H 67 N 92
N M 1

Output: Number of Potions Needed: 0 Distance to Chest: 20 Path to Chest: AGX

Code:

 class GraphEdge {
        private char from;
        private char to;
        private int weight;
    public GraphEdge(char from, char to, int weight) {
        this.from = from;
        this.to = to;
        this.weight = weight;
    }

    public class Graph {
        public static void main(String[] args) throws FileNotFoundException {
            DFS();}
    private static void DFS() 
        char source = 'A';// source vertex
        char destination = 'X';// destination vertex
        ArrayList<GraphEdge> edges = new ArrayList<GraphEdge>();// list to hold all edges
        HashSet<Character> uniqueVertices = new HashSet<Character>();// all unique vertices in map
        ArrayList<Character> allVertices = new ArrayList<Character>();// list to hold all vertices
        // System.out.println("Input File:");
        while (obj.hasNextLine()) {// while input file has lines left
            String line = obj.nextLine();
            System.out.println(line);
            String vertex[] = line.split(" ");// split the line into source vertex, destination vertex and the weight
            int i = 1;
        while (i < vertex.length) {
            GraphEdge edge = new GraphEdge(vertex[0].charAt(0), vertex[i].charAt(0),Integer.parseInt(vertex[i + 1]));
            edges.add(edge);
            i = i + 2;
            System.out.println(edge);
            System.out.println(vertex[0].charAt(0));
            System.out.println(vertex[1].charAt(0));
            //System.out.println(vertex[i].charAt(0));
            if (!uniqueVertices.contains(vertex[0].charAt(0))) {// if vertex not present in unique vertices map, add it
                // in map and arraylist
                uniqueVertices.add(vertex[0].charAt(0));
                allVertices.add(vertex[0].charAt(0));
                System.out.println(allVertices);
            }
            if (!uniqueVertices.contains(vertex[1].charAt(0))) {// if vertex not present in unique vertices map, add it
                // in map and arraylist
                uniqueVertices.add(vertex[1].charAt(0));
                allVertices.add(vertex[1].charAt(0));
                System.out.println(allVertices);
            }
            if (!uniqueVertices.contains(vertex[i].charAt(0))) {// if vertex not present in unique vertices map, add it
                // in map and arraylist
                uniqueVertices.add(vertex[i].charAt(0));
                allVertices.add(vertex[i].charAt(0));
                System.out.println(allVertices);
            }
        }
    }

The index variable i is incremented by 2 thus the last if block tries to access ahead of the last index. You need to add some guard code to break the loop before the last if block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM