Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:06:21+00:00 2026-06-05T03:06:21+00:00

im attempting to test a program in java and i am getting an array

  • 0

im attempting to test a program in java and i am getting an array index out of bounds exception that i dont believe should be thrown. have a look at this code and tell me if im missing something? eclipse is telling me the error is being thrown in the location where i have added a comment to show it

class maze{

private int cols; // number of columns in maze  
private int rows; // number of rows in maze
private String name;
private weightedGraph<Integer> graph;
private dijkstra solution;
public char[][] mazeStore;

public maze(String filename){

    try{

        FileReader r = new FileReader(filename);
        Scanner s = new Scanner(r);
        this.rows = s.nextInt();
        this.cols = s.nextInt();
        this.name = filename;


        this.mazeStore = new char[(2*rows)+1][(2*cols)+1];
        String line = s.nextLine();
        for(int k = 0; k < ((2*rows)+1); k++){

            char[] temp = line.toCharArray();

            for(int i = 0; i < temp.length; i++){
                mazeStore[k][i] = temp[i];
                line = s.nextLine();
            }
        }



        graph = new weightedGraph<Integer>(rows*cols); 


        for(int y = 1; y < 2*rows; y++){
            for(int x = 1; x < 2*cols; x++){
                if((x % 2 == 1) && (y % 2 == 0)){
                    if(mazeStore[x][y] != '-'){ // <<<<<<<<<<<<<<THIS IS WHERE THE ERROR IS THROWN 
                        int label = (x - 1) + (x / 2);
                        graph.addEdge(label, label+cols, 1);
                        graph.addEdge(label+cols, label, 1);
                    }
                }

                if((x % 2 == 0) && (y % 2 == 1)){
                    if(mazeStore[x][y] != '|'){
                        int label = ((x - 1) + (x / 2)) + (y / 2);
                        graph.addEdge(label, label+1, 1);
                        graph.addEdge(label+1, label, 1);
                    }
                }
            }
        }



        this.solution = new dijkstra(graph, 0); 


    }
    catch(FileNotFoundException e){
        System.err.println("FileNotFoundException: " + e.getMessage());
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:06:23+00:00Added an answer on June 5, 2026 at 3:06 am

    You initialized array

    new char[(2*rows)+1][(2*cols)+1] 
    

    but iterating it

    for(int y = 1; y < 2*rows; y++){//y row iterator
        for(int x = 1; x < 2*cols; x++){//x col iterator
    

    so it should be
    mazeStore[y][x] not mazeStore[x][y]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to test interactions with a Nexus server that requires authentication for the
I am attempting to compile and run a test C program in Xcode. This
I am currently attempting to revise a Java Homework program for my Data Structures
I am, in the included test program, attempting to copy a file from the
My Goal I am attempting to make a Java program in which a user
I am attempting to test user interaction with an off the shelf product. The
Attempting to implement a poor man's test of whether a process is still running
In our C# code, we already test if a variable is null before attempting
I'm a PHP developer, attempting to teach myself C#. I've got the following test
Attempting to run adt from an SSH session results in: Exception in thread main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.