java - 实例化一个新对象,从参数中获取一个indexoutofbounds,它是一个数组

标签 java arrays string substring indexoutofboundsexception

我的代码是这样的

package textExcel;

public class Spreadsheet implements Grid{
private int row = 20;
private int col = 12;
private Cell[][] sheet;
public Spreadsheet(){
      sheet = new Cell[20][12];
      for(int x = 0; x < sheet.length; x++) {
         for(int y = 0; y < sheet[x].length; y++) {
             sheet[x][y] = new EmptyCell();
         }
      }
}
public String processCommand(String command){ // processes a user command, returns string to display, must be called in loop from main
  if(command.equals("quit")) {
   return "";
  }
  else if(command.equals("clear")){
     sheet = new Cell[20][12];
     for(int x = 0; x < sheet.length; x++) {
         for(int y = 0; y < sheet[x].length; y++) {
             sheet[x][y] = new EmptyCell();
         }
     }
     return getGridText();
  }
  else if(((Character.isDigit(command.charAt(1))) && (Character.isLetter(command.charAt(0)))) && (command.length() == 2)) {
      int col = (int)command.charAt(0) - 65;
      int row = (int)command.charAt(1) - 49;
      return sheet[col][row].fullCellText();
  }
  else if((command.length()) > 5 && (command.substring(0,4).equals("clear"))) {
      int col = (int)command.charAt(6) - 65;
      int row = (int)command.charAt(7) - 49;
      sheet[col][row] = new EmptyCell();
      return getGridText();
  }
  else if(command.contains(" = ")) {
      int col = (int)command.charAt(0) - 65;
      int row = (int)command.charAt(1) - 49;
      System.out.println("col = " + col +", row = " + row);
      sheet[col][row] = new TextCell(command.substring(7, command.indexOf("\"")));
      System.out.println(sheet[col][row].abbreviatedCellText());
  }
return "";
}
public int getRows(){ // returns number of rows in grid
  return row;
}
public int getCols(){ // returns number of columns in grid
  return col;
}
public Cell getCell(Location loc){ // returns cell at loc
    int col = loc.getCol();
    int row = loc.getRow();
    return sheet[row][col];
}
public String getGridText(){ // returns entire grid, formatted as text for display
    String formatted;
    formatted = "   |A         |B         |C         |D         |E         |F         |G         |H         |I         |J         |K         |L         |\n";
    for(int x = 0; x < sheet.length - 11; x++) {
        String temp = (x+1) + "  |";
        for(int y = 0; y < sheet[x].length; y++) {
            System.out.println(sheet[x][y].abbreviatedCellText());
            temp = temp + sheet[x][y].abbreviatedCellText() + "|";
        }
        formatted = formatted + temp + "\n" ;
    }
    for(int x = 9; x < sheet.length; x++) {
        String temp = (x+1) + " |";
        for(int y = 0; y < sheet[x].length; y++) {
            System.out.println(sheet[x][y].abbreviatedCellText());
            temp = temp + sheet[x][y].abbreviatedCellText() + "|";
        }
        formatted = formatted + temp + "\n" ;
    }
    return formatted;
 }
}

当我在第 43 行输入 A1 = "apple"时,我收到 java.lang.StringIndexOutOfBoundsException。在使用调试器和打印语句查看索引问题后,我不确定索引问题来自哪里。我正在使用来自另一个类中使用的扫描器类的输入来运行该命令。

编辑: 对于运行所有这些所需的代码:

package textExcel;

//*******************************************************
// DO NOT MODIFY THIS FILE!!!
//*******************************************************

public interface Cell
{
    public String abbreviatedCellText(); // text for spreadsheet cell display, must be exactly length 10
public String fullCellText(); // text for individual cell inspection, not truncated or padded
}
package textExcel;

public class EmptyCell implements Cell{

public static final String CELL = "          ";
public String abbreviatedCellText(){ // text for spreadsheet cell display, must be exactly length 10
  return CELL;
   }
public String fullCellText(){ // text for individual cell inspection, not truncated or padded
  return CELL;
  }
    }
   package textExcel;

//*******************************************************
//DO NOT MODIFY THIS FILE!!!
//*******************************************************

public interface Grid 
{
// Grid interface, must be implemented by your Spreadsheet class
String processCommand(String command); // processes a user command, returns string to display, must be called in loop from main
int getRows(); // returns number of rows in grid
int getCols(); // returns number of columns in grid
Cell getCell(Location loc); // returns cell at loc
String getGridText(); // returns entire grid, formatted as text for display
}
package textExcel;

//*******************************************************
//DO NOT MODIFY THIS FILE!!!
//*******************************************************

public interface Location
{
// represents a location like B6, must be implemented by your 
SpreadsheetLocation class
int getRow(); // gets row of this location
int getCol(); // gets column of this location
}
package textExcel;

//Update this file with your own code.

public class SpreadsheetLocation implements Location
{
private String Loc;
private int col;
private int row;
public SpreadsheetLocation(String cellName)
{
    String loc = cellName;
    row = Integer.parseInt(loc.substring(1)) - 1;
    col = loc.charAt(0) - 'A';
}
@Override
public int getRow()
{
    // TODO Auto-generated method stub
    return row - 1;
}

@Override
public int getCol()
{
    // TODO Auto-generated method stub
    return col - 1;
}


}

package textExcel;

public class TextCell implements Cell {
private String cell;

public TextCell(String cell) {
    this.cell = cell;
}

@Override
public String abbreviatedCellText() {
    if(cell.length() > 11) {
        return cell.substring(0,9);
    }
    else {
        if(cell.length() == 0) {
            return cell + "          ";
        }
        else if(cell.length() == 1) {
            return cell + "         ";
        }
        else if(cell.length() == 2) {
            return cell + "        ";
        }
        else if(cell.length() == 3) {
            return cell + "       ";
        }
        else if(cell.length() == 4) {
            return cell + "      ";
        }
        else if(cell.length() == 5) {
            return cell + "     ";
        }
        else if(cell.length() == 6) {
            return cell + "    ";
        }
        else if(cell.length() == 7) {
            return cell + "   ";
        }
        else if(cell.length() == 8) {
            return cell + "  ";
        }
        else if(cell.length() == 9) {
            return cell + " ";
        }
        else {
            return "          ";
        }
    }
 }

 @Override
 public String fullCellText() {
    // TODO Auto-generated method stub
    return cell;
 }

 }

package textExcel;

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

// Update this file with your own code.

 public class TextExcel
 {

public static void main(String[] args)
{
    SpreadsheetLocation loc = new SpreadsheetLocation("L20");
    Spreadsheet test = new Spreadsheet();
    System.out.println(test.getGridText());
    System.out.println(test.getCell(loc));
    Scanner sc = new Scanner(System.in);
    System.out.println("input :");
    String input = sc.nextLine();
    test.processCommand(input);
    System.out.println("input :");
    input = sc.nextLine();
    test.processCommand(input);
    System.out.println("input :");
    input = sc.nextLine();
    test.processCommand(input);
    System.out.println("input :");
    input = sc.nextLine();
    test.processCommand(input);
    System.out.println("input :");
    input = sc.nextLine();
    test.processCommand(input);
 }
 }

最佳答案

由于您提供的代码缺少某些类定义,因此我无法运行它。我建议你在第43行进行调试,可能是substring函数导致了异常。最好使用StringUtils来进行字符串处理。

看看这一行。

sheet[col][row] = new TextCell(command.substring(7, command.indexOf("\"")));

当您输入:A1 =“apple” command.indexOf("\"") 的结果是 5。 所以 command.substring(7, 5) 导致异常。

起始位置7是什么意思?

将此行修改为 sheet[col][row] = new TextCell(command.substring(6, command.length()-1)); 你会得到结果:

col = 0, row = 0
apple 

我想这就是你想要的——打印引号之间的字符串。对吗?

关于java - 实例化一个新对象,从参数中获取一个indexoutofbounds,它是一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55084193/

相关文章:

java - 重启 RabbitMQ 主节点时 SimpleMessageListenerContainer 停止从 RabbitMQ 消费

java - Boggle 游戏板搜索程序问题

c - sizeof 作为字符串维度的段错误

c - 使用指针作为数组 - C

javascript - Javascript 中为什么引号前面有加号?

SwiftUI 使用本地化为句子中的一个单词添加颜色

java - JBoss 7.1 java.lang.IllegalArgumentException : Child container with name already exists 问题

Java错误消息 "... has private access in ..."

arrays - 当查找映射到小整数的常量时,使用 case 语句还是常量数组更快?

c++ - 将字符串传递给函数时出现 "conversion to const char*"错误