loading maps for a 2d game ERROR

Hi i'm trying to just get a basic map working but i keep getting and error and i'm not sure what is causing it.

Here is the code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

class MapExample extends JFrame {

ArrayList <String> lines = new ArrayList <String> ();

int lgth;

int bdth;

String userResponse;

int level;

char[][] myMap;

public MapExample() {

readMap();

parseMap();

}

public static void main(String args[]) {

System.out.println("Starting MapExample...");

MapExample mainFrame = new MapExample();

mainFrame.setSize(400, 400);

mainFrame.setTitle("MapExample");

mainFrame.setVisible(true);

}

public void readMap () {

FileReader readBase;

BufferedReader in;

String temp;

temp = "";

try {

readBase = new FileReader ("level1.map");

in = new BufferedReader (readBase);

temp = in.readLine();

while (temp != null) {

lines.add(temp);

temp = in.readLine();

}

}

catch (IOException except) {

JOptionPane.showMessageDialog (null, "A Horrible Error!");

}

}

public void parseMap () {

String aLine;

aLine = "";

char tmp;

tmp = 'a';

lgth = lines.size();

bdth = lines.get(0).length();

myMap = new char[lgth][bdth];

for (int i = 0; i < lines.size(); i++) {

aLine = lines.get(i);

for (int j = 0; j < aLine.length(); j++) {

tmp = aLine.charAt(j);

myMap[j] = tmp;

}

}

for (int i = 0; i < lgth; i++) {

for (int j = 0; j < bdth; j++) {

System.out.print (myMap[j]);

}

System.out.println();

}

}

public void paint(Graphics g) {

int gx, gy = 40;

for (int i = 0; i < lgth; i++) {

gx = 20;

for (int j = 0; j < bdth; j++) {

g.fillRect (gx, gy, lgth, bdth);

switch (myMap[j]) {

case '#' : g.setColor (Color.RED); break;

case '@' : g.setColor (Color.GREEN); break;

case '$' : g.setColor (Color.BLUE); break;

case '.' : g.setColor (Color.BLUE); break;

default : g.setColor (Color.BLACK); break;

}

g.fillRect (gx, gy, 20, 20);

gx += 20;

}

gy += 20;

}

}

Sorry about the layout of the code only way i could fit it in.

The error i keep getting is:

Starting MapExample...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14

at MapExample.parseMap(MapExample.java:99)

at MapExample.<init>(MapExample.java:33)

at MapExample.main(MapExample.java:38)

Process completed.

I'm not sure why it does this?

Also here is level1.map file:

"M^^^^#####" +

"M^^^^##" +

"M^^^^#$ #" +

"M^^### $##" +

"M^^# $ $ #" +

"M### # ## #^^^######" +

"M## ## ##### ..#" +

"M# $ $ ..#" +

"M##### ### #@## ..#" +

"M^^^^##########" +

"M^^^^#######"

}

Any help would be greatful!

Cheers Paul

[3242 byte] By [paul.ga] at [2007-11-27 4:20:44]
# 1

That "code" button just above the posting textarea inserts code tags so that your code is laid out more clearly.

The error is caused by one of your loops in parseMap trying to read a position in the array that is greater than the number of positions in the array. To find out what's causing that to happen, you could step through it in a debugger or just System.out.println the loop index every time. (As it stands just now, the code is missing the array indices in square brackets and is pretty unreadable.)

DavidKNa at 2007-7-12 9:27:49 > top of Java-index,Java Essentials,Java Programming...
# 2

I second the recommendation of putting your code within code tags.

See which is easier to read, this code without tags:

import java.util.ArrayList;

import java.util.Random;

class RandomLetters

{

private int size = 0;

private char startingChar = 'A';

private ArrayList<String> letterList;

private Random rand;

public RandomLetters(int size) throws Exception

{

this.size = size;

rand = new Random();

if (size % 2 != 0)

{

throw new Exception(

"RandomLetters requires an an even number int parameter");

}

resetList();

}

public RandomLetters(int size, char startingChar) throws Exception

{

this.size = size;

this.startingChar = startingChar;

rand = new Random();

if (size % 2 != 0)

{

throw new Exception(

"RandomLetters requires an an even number int parameter");

}

resetList();

}

public void setStartingChar(char startingChar)

{

this.startingChar = startingChar;

}

/**

* refills the list w/ random characters.

*

*/

public void resetList()

{

letterList = new ArrayList<String>();

for (char myChar = startingChar; myChar < startingChar + (size / 2); myChar++)

{

letterList.add(String.valueOf(myChar));

letterList.add(String.valueOf(myChar));

}

}

/**

* gets a random letter from letterList then removes that

* item from the list.

*

* @return

*/

public String getRandomLetter()

{

int randomInt = rand.nextInt(letterList.size());

String temp = letterList.get(randomInt);

letterList.remove(randomInt);

return temp;

}

}

and the same code with tags:

import java.util.ArrayList;

import java.util.Random;

class RandomLetters

{

private int size = 0;

private char startingChar = 'A';

private ArrayList<String> letterList;

private Random rand;

public RandomLetters(int size) throws Exception

{

this.size = size;

rand = new Random();

if (size % 2 != 0)

{

throw new Exception(

"RandomLetters requires an an even number int parameter");

}

resetList();

}

public RandomLetters(int size, char startingChar) throws Exception

{

this.size = size;

this.startingChar = startingChar;

rand = new Random();

if (size % 2 != 0)

{

throw new Exception(

"RandomLetters requires an an even number int parameter");

}

resetList();

}

public void setStartingChar(char startingChar)

{

this.startingChar = startingChar;

}

/**

* refills the list w/ random characters.

*

*/

public void resetList()

{

letterList = new ArrayList<String>();

for (char myChar = startingChar; myChar < startingChar + (size / 2); myChar++)

{

letterList.add(String.valueOf(myChar));

letterList.add(String.valueOf(myChar));

}

}

/**

* gets a random letter from letterList then removes that

* item from the list.

*

* @return

*/

public String getRandomLetter()

{

int randomInt = rand.nextInt(letterList.size());

String temp = letterList.get(randomInt);

letterList.remove(randomInt);

return temp;

}

}

Also, post the total error message, not just that errors occur.

good luck!

/pete

petes1234a at 2007-7-12 9:27:49 > top of Java-index,Java Essentials,Java Programming...