I'm trying to take a text file with 20x15 numbers(or characters, doesn't matter) into a 2d array. I just can't seem to get it to work. Can you help me fix it? Thanks

FileInputStream fis = new FileInputStream("MapFile.txt");
			
			while (fis.available() > 0) {
				
				for (int x = 0; x < MAP_X; x++) {
					for (int y = 0; y < MAP_Y; y++) {
						
						map[x][y] = (char) fis.read();
						
						System.out.println(map[x][y]);
	
					}
				}
			}

Recommended Answers

All 2 Replies

Paste the entire code. Is there an error ? Tell exactly what it is that is not working.

How is your data stored in the text file? You most likely would need to read in a string and parse out an integer, double, etc. If you're storing more than one character per entry you'll need to do something similar.

Also, how are you separating each item in the text file? Check that you're actually just reading one element at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.