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

  • Home
  • SEARCH
  • 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 7458631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:28:23+00:00 2026-05-29T13:28:23+00:00

I’m new to java, and I’m currently programming a pacman as a class project.

  • 0

I’m new to java, and I’m currently programming a pacman as a class project. The game is played on an array[8][8]; I’ve got to read a .txt file that includes walls, ghosts and their position in the array, and show them there. I’ve used Scanner, StringTokenizer and nextInt. When I compile the file, it doesn’t give any errors, but when running it, java.lang.ArrayIndexOutOfBoundsException: 8 shows up; by the error name I can infer there’s some object outside the array, but I can’t seem to identify which one. Here’s the code:

public class JuegoPacman
{
     private Elemento _mat[][];



    public JuegoPacman()
    {

    }



    public void Escanear() throws FileNotFoundException
    {
        Scanner sc=new Scanner(new File("inicio.txt"));
        while(sc.hasNext())
        {
            String token = sc.next();

            if (token.equals("Pared"))
            {
                int i=sc.nextInt() -1;
                int j=sc.nextInt() -1;

                _mat[i][j] = new Pared(i,j);
            }

            else if(token.equals("Fantasma"))
            {
                int i=sc.nextInt();
                int j=sc.nextInt();

                _mat[i][j]= new Fantasma(i,j);
            }
        }
    }

    public void crearMatriz()
    {
        for (int i=0; i<_mat.length;i++)
        {
            for (int j=0;i<_mat[i].length;j++)
            {
                System.out.print(_mat[i][j]);
            }
        }
    }

    public void jugar() throws FileNotFoundException
    {
        _mat=new Elemento[8][8];
        Escanear();
        crearMatriz();
    }
}

This is the main class:

public class Aplicacion {


    public Aplicacion()
    {
    }


    public static void main(String[] args) throws FileNotFoundException
    {
       JuegoPacman juego = new JuegoPacman();
       juego.jugar();
    }
}

And here’s the .txt i’m trying to read:

Pared 2 2 
Pared 3 2 
Pared 4 2 
Pared 6 2 
Pared 7 2 
Pared 5 4 
Pared 2 5 
Pared 4 5 
Pared 6 5 
Pared 2 6 
Pared 4 6 
Pared 6 6 
Pared 2 7 
Pared 5 7 
Fantasma 1 3
Fantasma 1 8
Fantasma 8 8

In advance, Thank you very much.

EDIT:

Substracting 1 of each of the location numbers still gives me the same error.

ERROR:
nullnullFantasma@a31e1bnullnullnullnullFantasma@10da5ebException in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8
at JuegoPacman.crearMatriz(JuegoPacman.java:58)
at JuegoPacman.jugar(JuegoPacman.java:67)
at Aplicacion.main(Aplicacion.java:22)

  • 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-05-29T13:28:24+00:00Added an answer on May 29, 2026 at 1:28 pm

    The numbering arrangements in java start from zero. If you create an array of 8 positions, then the positions will be from 0 to 7. If you try to find position 8, it will give you that error.

    what to do to fix your code is

                 int i = sc.nextInt () - 1;
                 int j = sc.nextInt () - 1;
    

    must be replaced on both sides

    public void Escanear() throws FileNotFoundException
    {
        Scanner sc=new Scanner(new File("inicio.txt"));
        while(sc.hasNext())
        {
            String token = sc.next();
    
            if (token.equals("Pared"))
            {
                int i=sc.nextInt() -1;
                int j=sc.nextInt() -1;
    
                _mat[i][j] = new Pared(i,j);
            }
    
            else if(token.equals("Fantasma"))
            {
                int i=sc.nextInt() - 1;
                int j=sc.nextInt() - 1;
    
                _mat[i][j]= new Fantasma(i,j);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.