为什么会出现StringOutofBoundException?

时间:2015-03-15 03:03:39

标签: java

我的代码需要从文本文件中读取信息。文本文件如下所示:

X...................
....................
....................
....................
....................
....................
....................
....................
....................
..X.................

是的,我需要获取行数和列数,并确定“X”的数量和位置。我差不多完成了,除了我的第二个构造函数在行中给我一个StringOutOfBoundException:

treasureLocations[location] = new Coord(i, j);

我只需要第二个构造函数的帮助。请问smb请帮帮我吗?

import java.util.Scanner;   // Required to get input
import java.io.File;        // Required to get input from files



// A 2D treasure map which stores locations of treasures in an array
// of coordinates
public class TreasureMap{
    int rows, cols;     // How big is the treasure map
    Coord [] treasureLocations; // The locations of treasures

    Scanner kbd = new Scanner(System.in);
  // Prompt the user for info on the treasure map and then create it
  // COMPLETE THIS METHOD
  public TreasureMap(){

      int numberOfTreasures = 0;
      System.out.println("Enter map size (2 ints): ");
      rows = kbd.nextInt(); cols = kbd.nextInt();
      System.out.println("Enter number of treasures (1 int): ");
      numberOfTreasures = kbd.nextInt();

      treasureLocations = new Coord[numberOfTreasures];

      for (int i = 0; i < numberOfTreasures; i++)
      {
          System.out.println("Enter treasure " + i + " location (2 ints): ");
          rows = kbd.nextInt(); cols = kbd.nextInt();
          treasureLocations[i] = new Coord(rows, cols);
      }
  }



  // Read the string representation of a map from a file
  // COMPLETE THIS METHOD
  public TreasureMap(String fileName) throws Exception{
      rows = 0;
      cols = 0;
      int treasures = 0;
      char x = 'X';


      Scanner data = new Scanner(new File(fileName));

      while(data.hasNextLine())
      {
          String line = data.nextLine();
          for (int i = 0; i < line.length(); i++)
          {
              if(x == line.charAt(i))
              {
                  treasures++;
              }

          }
          cols = line.length();
        rows++;
      }


      int location = 0;
      treasureLocations = new Coord[treasures];
      Scanner temp = new Scanner (new File(fileName));
      while(temp.hasNextLine())
      {
          String line = temp.nextLine();
          for(int i = 0; i < rows; i++)
          {
              for (int j = 0; j < cols; j++)
              {
                  if(x == line.charAt(j))
                  { 
                      treasureLocations[location] = new Coord(i, j);
                      location++;
                  }
              }
          }
      }


      }





  // true if there is treasure at the given (r,c) coordinates, false
  // otherwise
  // This method does not require modification
  public boolean treasureAt(int r, int c){
    for(int i=0; i<treasureLocations.length; i++){
      Coord coord = treasureLocations[i];
      if(coord.row == r && coord.col == c){
    return true;
      }
    }
    return false;
  }

  // Create a string representation of the treasure map
  // This method does not require modification
  public String toString(){
    String [][] map = new String[this.rows][this.cols];
    for(int i=0; i<rows; i++){
      for(int j=0; j<cols; j++){
            map[i][j] = ".";
      }
    }
    for(int i=0; i<treasureLocations.length; i++){
      Coord c = treasureLocations[i];
      map[c.row][c.col] = "X";
    }
    StringBuilder sb = new StringBuilder();
    for(int i=0; i<rows; i++){
      for(int j=0; j<cols; j++){
    sb.append(map[i][j]);
      }
      sb.append("\n");
    }
    return sb.toString();
  }

}

1 个答案:

答案 0 :(得分:0)

您不应该像使用for i那样使用i。您的temp.nextLine()表示当前行,不是吗?因此,每次输入一行(i)时,您的int i=0; while(temp.hasNextLine()) { String line = temp.nextLine(); for (int j = 0; j < cols; j++) { if(x == line.charAt(j)) { treasureLocations[location] = new Coord(i, j); location++; } } } ++i; } 都必须添加一行。

{{1}}