java.lang.stringoutofboundsexception长度=40,startRegion=21,regionLength=-15
我是开发新手,现在在.substring得到一个越界异常 长度=40,起始区域=21,区域长度=15 该字符串由40个字符组成,由条形码阅读器进行扫描。条形码是(01)90000055097706(3103)018130(10)24022402(30)00 扫描时显示为019000005509770631030181301020224023000 请帮忙 编辑:查找字符串的018130部分。感谢您指出startIndex和endIndex。这就是我所缺少的,虽然语法是startIndex,length 再次感谢java.lang.stringoutofboundsexception长度=40,startRegion=21,regionLength=-15,java,android,Java,Android,我是开发新手,现在在.substring得到一个越界异常 长度=40,起始区域=21,区域长度=15 该字符串由40个字符组成,由条形码阅读器进行扫描。条形码是(01)90000055097706(3103)018130(10)24022402(30)00 扫描时显示为019000005509770631030181301020224023000 请帮忙 编辑:查找字符串的018130部分。感谢您指出startIndex和endIndex。这就是我所缺少的,虽然语法是startIndex,len
String scannedAmount = scanAmount.getText().toString();
if(scannedAmount.length() > 10)
{
scannedAmount = scannedAmount.substring(21, 6); // removes only the weight value in the string
barcodeAmount = parseDouble(scannedAmount);
barcodeAmount = barcodeAmount * conversion;//changes the weight from kg to lb
} else if(scannedAmount.length()<=10) {
barcodeAmount = parseDouble(scannedAmount);
}
String scannedAmount=scanAmount.getText().toString();
如果(scannedAmount.length()>10)
{
scannedAmount=scannedAmount.substring(21,6);//仅删除字符串中的权重值
条形码金额=parseDouble(扫描金额);
barcodeAmount=barcodeAmount*conversion;//将重量从kg更改为lb
}else if(scannedAmount.length()scannedAmount=scannedAmount.substring(21,6)
字符串子字符串结束索引错误。下面是jdk代码。结束索引应大于开始索引
<pre>int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
</pre>
int subcn=endIndex-beginIndex;
如果(子项<0){
抛出新StringIndexOutOfBoundsException(SubCN);
}
如oracle文档网站所列,子字符串的工作原理如下:
公共字符串子字符串(int beginIndex,
内部索引)
返回作为此字符串的子字符串的新字符串。子字符串
从指定的beginIndex开始,并延伸到
索引endIndex-1。因此子字符串的长度为
endIndex beginIndex
示例:
scannedAmount = scannedAmount.substring(6, 21);
您需要将第一个索引作为第一个参数传递,将第二个索引作为第二个参数传递
它抛出stringOutOfBoundsException,因为您试图在-15索引(6-21)处对字符串进行索引
将代码更改为如下所示:
substring(startIndex, endIndex)
您将不再从这一行获得异常
至于代码的功能,我不能说,因为您没有给出应该发生什么的上下文。我相信您更熟悉的语法是C#,即子字符串(startIndex,lengthOfSubstring)
,但在java中正确的语法是
scannedAmount = scannedAmount.substring(21, 27);
其中返回的字符串
是字符串的一部分,从开始索引
一直到结束索引
(不包括在内)。因此,如果您希望字符串中的六个字符从索引21开始,您应该这样做
您想从该字符串中获得什么?:返回一个新字符串,该字符串是该字符串的子字符串。该子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。因此,该子字符串的长度为endIndex beginIndex。编辑您的答案以在注释中插入代码。搞定它!谢谢Chris
scannedAmount = scannedAmount.substring(21, 27);