【问题标题】:Java string index out of rangeJava 字符串索引超出范围
【发布时间】:2013-04-05 09:03:52
【问题描述】:

所以我得到的字符串索引超出范围意味着我超出了集合的范围,但我不太确定我的代码是如何做到这一点的。

此代码应该从包含 3 个整数的文本文件中读取(前两个与此相关,分别为行数和列数),然后是一系列字符。因此,它首先读取并保存前三个数字,然后将文件的其余部分转换为字符串。然后,它通过使用文本文件的尺寸设置一个字符数组,然后用文本文件的字符逐个字符地填充这些值。

但是,当我尝试打印代码时,遇到字符串索引超出范围错误,无法找到问题。

这是代码:

  import java.util.*;  
  import java.io.*;

public class SnakeBox {
// instance variables
  private char[][] box;
  private int snakeCount;
  private int startRow, startCol;
  private int endRow, endCol;
    private int rows, cols, snakes;
  Scanner keyboard = new Scanner(System.in);

/** Create and initialize a SnakeBox by reading a file.
    @param filename the external name of a plain text file
*/
  public SnakeBox(String fileName) throws IOException{
     Scanner infile = new Scanner(new FileReader(fileName));


     int count = 0;
     String s = "";
     rows = infile.nextInt();
     cols = infile.nextInt();
     snakes = infile.nextInt();
        infile.nextLine();
     box = new char[rows][cols];
     while (infile.hasNext()) {
        s += infile.next();
     }
     for (int i = 0; i < rows; i++) {

        for (int j = 0; j < cols; j++) {
           count++;
           char c = s.charAt(count);           
           box [i][j] = c;
        }
     }
  } 

文本文件是:

    16 21 4
+++++++++++++++++++++
+                   +
+         SSSSS     +
+    S   S          +
+    S    SS        +
+    S      S       +
+ S  S       SS     +
+  SS  S            +
+     S             +
+    S  SSSSSS      +
+   S         S     +
+  S           S    +
+ S     SSS   S     +
+      S     S      +
+       SSSSS       +
+++++++++++++++++++++

谢谢。

【问题讨论】:

    标签: java for-loop multidimensional-array indexof


    【解决方案1】:

    改变

    count++;
    char c = s.charAt(count);  
    

    char c = s.charAt(count);   
    count++;
    

    编辑:另一个问题是 Scanner.next() 返回下一个标记,忽略所有空格。查看 Achintya Jha 的答案

    【讨论】:

    • 确实,当您在最后一个索引上时,这会导致问题。假设您在索引 10(最后一个)上,然后您将尝试获取不存在的 charAt() 索引 11。大斑点
    • 啊,谢谢,愚蠢的错误。但是,再次运行该文件仍然会使我在同一位置出现字符串索引越界异常。
    • @user2248256 嗯。我会尝试打印出 s 的值,它的长度,然后是 count 的值,看看是怎么回事
    【解决方案2】:

    问题不是上面所说的: 在给定的文件中有将近 59 个标记,总共等于 117 个字符(我手动计算)。而count的最大值将达到21*16 =336。

    如果名为“infile.dat”的输入文件具有以下格式:

    你好 1234
    CS学生再见6556

    它有六个标记,我们可以使用以下代码读取它们:

    while (filein.hasNext()) {      // while there is another token to read
        String s = filein.next();   // reads in the String tokens "Hello" "CSstudents" 
        String r = filein.next();   // reads in the String tokens "There" "goodbye"
        int x = filein.nextInt();   // reads in the int tokens 1234  6556 
    
    }
    

    注意 Scanner 对象如何跳过所有空白字符到下一个标记的开头。 所以我的建议是使用Scanner#hasNextLine() & Scanner#nextLine()

    while (infile.hasNextLine) {
        s += infile.nextLine();
    }
    

    【讨论】:

    • 检查文档docs.oracle.com/javase/1.5.0/docs/api/java/util/… 就像 Achintya 所说的 next() 返回一个令牌。文件中的所有空格都会被忽略。
    • 啊,谢谢,这是个大问题,我怎样才能保存数组中的空白空间?
    • 我从未使用过扫描仪,但 readLine 可能会有所帮助,因为据我了解文档,它没有读取令牌。 while (scanner.hasNextLine()) { someString = someString +scanner.nextLine(); } 然后将字符串转换为您的数组。
    • 非常感谢大家。将其更改为读取行而不是下一个标记修复了它,我误解了 next() 做了什么。还要感谢其他答案,因为这也是一个问题。
    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 2012-01-15
    • 2014-06-12
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    相关资源
    最近更新 更多