a 2D char array from a text file?

How could I make an array of characters be entered from a text file. At the moment it has the numbers and space in a 100*100 grid (map.txt, seperated by enters for each line), although I would like to make a 1000*1000 grid using all the alpha-numeric characters later(maby symbols too).

But anyway I have this:

//in init:

try{

BufferedReader mapReader =new BufferedReader(new FileReader("Map.txt"));

}catch (FileNotFoundException exc){

System.out.println("A map file was not found.");

}

//and

board =newchar[boardSize+1][boardSize+1][2];

tempChar =newchar[boardSize*boardSize];

//later I call this to input the board

publicvoid resetBoard(){

mapReader.read(tempChar);

try{

for(i=0;i<10000;i++){

board[(int)Math.floor(i/100)][i-((int)Math.floor(i/100))][0] = tempChar[i];

board[(int)Math.floor(i/100)][i-((int)Math.floor(i/100))][1] = tempChar[i];

}

}catch(ArrayIndexOutOfBoundsException exc){ ;}

}

Any ideas on how I can make this actually read the file, it does NOT give me the "a map file was not found error, and it gives me a

E:\Java\DungeonFighter\DungeonFighter.java:335: cannot resolve symbol

symbol : variable mapReader

location: class DungeonFighter

mapReader.read(tempChar);

^

error

Thanks Zeb

PS. It says that I override a deprecated API. I don't really care, but I think that it might be a good thing to know what I am overriding.

[2569 byte] By [Zebediah] at [2007-9-30 21:46:24]
# 1

Something like the following should work:

int[][][] values = new int[100][100][2];

FileInputStream fileIn = new FileInputStream("map.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(fileIn));

for (int rowCurrent = 0; rowCurrent < 100; rowCurrent++) {

String line = reader.readLine();

StringTokenizer tokenizer = new StringTokenizer(line, " ");

for (int colCurrent = 0; colCurrent < 100; colCurrent++) {

int currentValue = Integer.parseInt(tokenizer.nextToken());

values[rowCurrent][colCurrent][0] = currentValue;

values[rowCurrent][colCurrent][1] = currentValue;

}

}

- Saish

"My karma ran over your dogma." - Anon

Saish at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 2
Did you know that (in C and the derived languages like C++ and Java) if i is ant int, then /100 is an integer too? Appearently not.
BIJ001 at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 3
I will try some of those methods, but I was trying for characters, is that possible?also what is that /100 thing? ("if i is an int, /100 is an integer too")?For now, I just assigned spaces to everythink to work on the other methods :) I can move anywhere!
Zebediah at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 4

If you want characters (and they are space delimited within your file), then simply remove the Integer.parseInt() code and replace with the following:

tokenizer.nextToken().charAt(0);

If you want to convert integer values to characters (ala Ascii codes), simply use:

(char) Integer.parseInt(tokenizer.nextToken());

- Saish

"My karma ran over your dogma." - Anon

Saish at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 5

Thanks, the type of data that it would be entering would be like this

1111111111

1000011111

1110000111

11A11100G1

1100000111

1101111111

1100000111

1111110D11

11S0000111

1111111111

not actually anything there, just a path, but this is the type of entered thing(also, it should be viewed in monospace font.

Zebediah at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 6

> How could I make an array of characters be entered

> from a text file. At the moment it has the numbers

> and space in a 100*100 grid (map.txt, seperated by

> enters for each line), although I would like to make a

> 1000*1000 grid using all the alpha-numeric characters

> later(maby symbols too).

>

> But anyway I have this:

> > //in init:

> try {

> BufferedReader mapReader = new BufferedReader(new

> FileReader("Map.txt"));

> } catch (FileNotFoundException exc) {

> System.out.println("A map file was not found.");

> }

> //and

> board = new char[boardSize+1][boardSize+1][2];

> tempChar = new char[boardSize*boardSize];

>

> //later I call this to input the board

> public void resetBoard() {

> mapReader.read(tempChar);

> try {

> for(i=0;i<10000;i++) {

> board[(int)Math.floor(i/100)][i-((int)Math.floor(i/100)

> ][0] = tempChar[i];

> board[(int)Math.floor(i/100)][i-((int)Math.floor(i/100)

> ][1] = tempChar[i];

> }

> } catch(ArrayIndexOutOfBoundsException exc) { ; }

> }

>

>

> Any ideas on how I can make this actually read the

> file, it does NOT give me the "a map file was not

> found error, and it gives me a

> E:\Java\DungeonFighter\DungeonFighter.java:335: cannot

> resolve symbol

> symbol : variable mapReader

> location: class DungeonFighter

> mapReader.read(tempChar);

> ^

> error

>

> Thanks Zeb

>

> PS. It says that I override a deprecated API. I don't

> really care, but I think that it might be a good thing

> to know what I am overriding.

If you dont declare the object mapReader at class level you cannot see it outside the try/catch block.

MassimilianoOnorato at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 7
Best of luck.- Saish"My karma ran over your dogma." - Anon
Saish at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 8

Thanks, I thaught that it seemed kinda wierd that that would work while *arrays* need to be declared, so in the declarations where i say "int i;" and stuff i say

BufferedReader mapReader;

FileReader mapFileReader;

I also would change the init item appropriatly for it to make sense

interestingly enough, I also have a zoom thing (zoom is the size of a square in pixels) it works fine, i can zoom in and out in 5,10,25, and 50 pxl squares with no problem. when i do the last zoom out(to one) though, it just doesn't like it. (the thread locks up, appletviewer.exe takes between 97 and 99 % of my processing power, and nothing happens... )

Very Interesting........

Zebediah at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 9

I have another problem now. The string seems to not be right, as I changed by paint thing to display the character, rather than the color in the correct box. the columns are the same, identical to the first entry in the corresponding row. I do not know why. any clue?

here is the code: public void resetBoard() {

try {

mapFileReader = new FileReader("Map.txt");

mapReader = new BufferedReader(mapFileReader);

mapRead = true;

} catch (FileNotFoundException exc) {

System.out.println("A map file was not found.");

mapRead = false;

}

for (int rowCurrent = 0; rowCurrent < boardSize; rowCurrent++) {

try {

line = mapReader.readLine();

} catch(IOException exc) { ; }

tokenizer = new StringTokenizer(line, "\n\r");

for (int colCurrent = 0; colCurrent < boardSize; colCurrent++) {

try {

char currentValue = tokenizer.nextToken().charAt(colCurrent);

board[rowCurrent][colCurrent][0] = currentValue;

board[rowCurrent][colCurrent][1] = currentValue;

} catch(NoSuchElementException exc) { ; }

}

}

}

if my file is

10000

20000

30000

40000

50000

my output is :

12345

12345

12345

12345

12345

(of course it is a larger thing, but this is the pattern.

Any ideas?

Zebediah at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...
# 10

Grrr... I wish there was a edit button.

My computer-teacher-to-be suggested that I can simply use the readLine() method to imput the string to the array, as I want ALL the characters(other then enter) in the array(more characters, more possibilities :).

for(i=0; i<boardSize; i++) {

board[i][][0] = mapReader.readLine();

board[i][][1] = board[i][][0];

}

Maby it'll work :)>

Zebediah at 2007-7-7 3:15:31 > top of Java-index,Administration Tools,Sun Connection...