Java中的字符串操作导致“字符串索引超出范围”
我有一个很长的数据包大小列表,需要重新编写,我不想手工编写,所以我要为它编写一个程序Java中的字符串操作导致“字符串索引超出范围”,java,string,Java,String,我有一个很长的数据包大小列表,需要重新编写,我不想手工编写,所以我要为它编写一个程序 public static OutcommingPacket aClass198_1993 = new OutcommingPacket(68, 8); 这是其中一行的例子,我需要做的是让程序转到68,存储在数据包字符串中,然后得到数字8,并存储在大小字符串中 这是到目前为止我的代码 public class PacketSizeFixer { public static final String IN =
public static OutcommingPacket aClass198_1993 = new OutcommingPacket(68, 8);
这是其中一行的例子,我需要做的是让程序转到68,存储在数据包字符串中,然后得到数字8,并存储在大小字符串中
这是到目前为止我的代码
public class PacketSizeFixer {
public static final String IN = "./out/OldPacketSizes.txt";
public static final String OUT = "./out/PacketSizesFormatted.txt";
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(IN));
BufferedReader writer = new BufferedReader(new FileReader(OUT));
String line;
String packet, size;
while ((line = reader.readLine()) != null) {
packet = line.substring(line.indexOf("new OutcommingPacket(", line.indexOf(", ")));
size = line.substring(line.indexOf(", "), line.indexOf(");"));
}
}
}
我不确定我的方法是否正确,因为我一直在获取超出范围的字符串索引
请帮忙
顺便说一下,并非所有的数据包都有相同的名称,有些更长,有些更短,数据包可以是两位数,大小也可以是两位数。请帮忙 您可能会遇到此错误,因为您正在查找的子字符串未找到,返回-1,然后您在不检查返回索引的情况下调用子字符串。 尝试:
您可能会遇到此错误,因为找不到要查找的子字符串返回-1,然后您调用子字符串时没有检查返回的索引。 尝试:
我在这里假设了很多
while ((line = reader.readLine()) != null) {
String pair = line.substring(line.lastIndexOf("("), line.lastIndexOf(")"));
String values[] = pair.split(","); //values[0] == packet, values[1] == size
}
我在这里假设了很多
while ((line = reader.readLine()) != null) {
String pair = line.substring(line.lastIndexOf("("), line.lastIndexOf(")"));
String values[] = pair.split(","); //values[0] == packet, values[1] == size
}
从您的示例中,听起来您想要提取的信息是:
#,#
那么为什么不使用正则表达式呢
CharSequence inputStr = "new OutcommingPacket(68, 8)";
String patternStr = "(\\d+),(\\d+)";
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound) {
// Get all groups for this match
for (int i=0; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
}
}
注意:我没有测试过这个精确的模式,但至少应该接近正确…从您的示例中,听起来您想要提取的信息是:
#,#
那么为什么不使用正则表达式呢
CharSequence inputStr = "new OutcommingPacket(68, 8)";
String patternStr = "(\\d+),(\\d+)";
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound) {
// Get all groups for this match
for (int i=0; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
}
}
注意:我还没有测试这个精确的模式,但它至少应该接近正确…add:System.out.printlnindex1=+index1+;和index2=+index2;只是为了确保…添加:System.out.printlnindex1=+index1+;和index2=+index2;为了确保……请将实际异常和堆栈跟踪作为问题的一部分发布。这是一个本地化的调试问题,如果人们能看到问题,可能会更容易解决。“Outcoming”用一个“m”拼写。您正在为您的作者使用阅读器。如果找不到子字符串,则忽略indexOf的-1返回。您需要将正在搜索的字符串长度添加到子字符串调用的起始索引中。请将实际异常和堆栈跟踪作为问题的一部分发布。这是一个本地化的调试问题,如果人们能看到问题,可能会更容易解决。“Outcoming”用一个“m”拼写。您正在为您的作者使用阅读器。如果找不到子字符串,则忽略indexOf的-1返回。您需要将正在搜索的字符串的长度添加到子字符串调用的起始索引中。我认为,由于OP正在解析看似Java代码的内容,因此输入将遇到格式化程序将部分代码包装到下一行的问题。我还认为模式字符串需要确保它只查找位于指定构造函数模式中的数字,并处理Java允许空白的可能空白。当然,最后一点完全不符合OP的方法。我认为,因为OP正在解析看起来是Java代码的内容,所以输入将遇到格式化程序将部分代码包装到下一行的问题。我还认为模式字符串需要确保它只查找位于指定构造函数模式中的数字,并处理Java允许空白的可能空白。当然,最后一点根本不符合OP的方法。