2016-03-06 50 views
0

我在尝试填充我创建的数组时遇到了麻烦。看来不管我如何设置循环,它仍然会抛出错误。纵观我的代码,我看不出我出错的地方,有人可以向我提供线索吗?数组越界异常,不明显,其中

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

    /************************************************************************* 
    * Name  : 
* Username : 
* Description : 
*************************************************************************/ 



    public class World{ 

    String fileName; 
    Tile[][] map; 
    int width,height; 
    int x_coor, y_coor; 

public World(String inFileName){ 
    this.fileName = inFileName; 

    //Create all the tiles 

    try { 

     //create the scanner in order to read the file parameters 
     Scanner scanner = new Scanner(new File(fileName)); 
     //Get the height and width of the map 
     int height, width; 
     width = scanner.nextInt(); 
     height = scanner.nextInt(); 
     Tile[][] map = new Tile[width][height]; 
     //testing 
     System.out.println("Width is: " +width); 
     System.out.println("Height is: " + height); 
     //create blank map 
     for(int i = 0; i < height-1; i++){ 
      for(int j = 0; j < width -1; j++){ 
       map[i][j] = new Tile("Blank"); 
       System.out.println("System created point:" + i + j); 
      } 
     } 

     //instantiate the map, and fill it 
     for(int i = 0; i < height ; i++){ 
      for(int j = 0; j < width - 1; j++){ 
       //Testing code 
       System.out.print(j); 
       //create tiles 
       map[i][j] = new Tile(scanner.next()); 
      } 
      //Testing code 
      System.out.println(); 
     } 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    } 
     private void draw(){ 

    } 

    public static void main(String [] args) 
    {  


     World world = new World(args[0]); 
     world.draw();  
    } 
} 
+0

请提供错误你 – bibi

回答

0

for(int i = 0; i < height ; i++)第二嵌套循环for//instantiate the map, and fill it)你没有在1

for(int i = 0 ; i < height - 1 ; i++) 

减少height此外,您在Tile[][] map = new Tile[width][height];混合widthheight。它应该是

Tile[][] map = new Tile[height][width]; 
+0

固定,它仍然在 \t \t \t \t地图[I]抛出一个错误后[J] =新的瓷砖( “空”); – Kevin

+0

@Kevin发布了一个新问题,因为解决了这个问题。 –

+0

@Kevin你还有问题吗? – Guy