2 Replies - 311 Views - Last Post: 19 December 2013 - 02:52 AM Rate Topic: -----

#1 jaredc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-December 13

Reading a text file into a 2D array

Posted 18 December 2013 - 11:37 PM

I am trying to read a text file into a 2D array for use in a top-down dungeon game. I can't figure out what is going wrong with how I am attempting to do this though.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class readTextTest {
	static int[][] map;// = new int[11][22];
	

    private static int[][] constructMap(String filename) throws FileNotFoundException {
            Scanner scan = new Scanner(new File(filename)); // make a new Scanner
            int height = scan.nextInt(); // retrieve the height value (first line) from your file
            scan.nextLine(); // skip to the next line
            int width = scan.nextInt(); // retrieve width
            scan.nextLine(); // skip
            int[][] newMap = new int[height][width]; // build a new 2D Array of size height by width
		
	    for (int y = 0; y < width; y++) {
		    for (int x = 0; x < height; x++) {
			    newMap[y][x] = scan.nextInt();
			    scan.nextLine();
		    }
	    }
	    return newMap;
    }


	public static void main(String args[]) throws FileNotFoundException {
		constructMap("matrixText.txt");
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				System.out.print(map[i][j]);
			}
			System.out.println();
		}
	}
}



The text file I am reading has this as the contents, the first two digits are the dimensions, followed by the array:
11
22
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1



When I run I am getting this error:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at readTextTest.constructMap(readTextTest.java:27)
at readTextTest.main(readTextTest.java:66)

Thanks for any insight.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a text file into a 2D array

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Reading a text file into a 2D array

Posted 19 December 2013 - 12:18 AM

You are telling it to take a new line after each number.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Reading a text file into a 2D array

Posted 19 December 2013 - 02:52 AM

Quote

You are telling it to take a new line after each number.

Yes. You don't need to do that as nextInt will already consume the whitespace in the line feed
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1