Reputation: 9
I am getting a string index out of bound exception while decoding string pattern a-695b-36c158d-666e-1326f144. I want to extract values between a & b, b & c, c & d.
This code prints values sometimes properly but fails sometimes. I dont understand why this exception occurs sometimes.
Below is my code:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[128];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
int buflen=buffer.length;
String str = new String(buffer, "UTF-8"); // prints character
Log.d("Data Transmitted", str);
int spaceIndex = str.indexOf(" ");
if (spaceIndex != -1)
{
str = str.substring(0, spaceIndex);
}
Log.d("Data Edited", str);
boolean v1=str.contains("a");
boolean v2=str.contains("b");
boolean v3=str.contains("c");
boolean v4=str.contains("d");
if(v1==true && v2==true && v3==true && v4==true){
mEmulatorView.write(buffer, bytes);
String op1=str.substring(str.indexOf("a")+1, str.indexOf("b"));
Log.d("output", op1);
String op2=str.substring(str.indexOf("b")+1, str.indexOf("c"));
Log.d("output", op2);
String op3=str.substring(str.indexOf("c")+1, str.indexOf("d"));
Log.d("output", op3);
}
else{
mEmulatorView.write(buffer, bytes);
}
// Send the obtained bytes to the UI Activity
//mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
I always get data in this form: 12-12 12:43:54.681: D/Data Transmitted(23778): a-695b-36c158d-666e-1326f144
12-12 12:43:54.681: D/Data Transmitted(23778): a-684b-75c35d-659e-838f119
12-12 12:43:54.681: D/Data Transmitted(23778): a-674b-353c238d-658e-556f148
Exception:
> E/AndroidRuntime(23778): FATAL EXCEPTION: Thread-2313
> E/AndroidRuntime(23778): Process: es.pymasde.blueterm, PID: 23778
> E/AndroidRuntime(23778):
> java.lang.StringIndexOutOfBoundsException:length=128; regionStart=29;
> regionLength=-26 E/AndroidRuntime(23778): at
> java.lang.String.startEndAndLength(String.java:588)
> E/AndroidRuntime(23778): at
> java.lang.String.substring(String.java:1475)
Upvotes: 0
Views: 142
Reputation: 414
You can use regex to parse string : -
String str = "a-695b-36c158d-666e-1326f144";
boolean v1=str.contains("a");
boolean v2=str.contains("b");
boolean v3=str.contains("c");
boolean v4=str.contains("d");
if(v1==true && v2==true && v3==true && v4==true){
String aToB = str.replaceAll(".*a", "").replaceAll("b.*", "");
System.out.println(aToB);
String bToc = str.replaceAll(".*c", "").replaceAll("d.*", "");
System.out.println(bToc);
String cTod = str.replaceAll(".*d", "").replaceAll("e.*", "");
System.out.println(cTod);
}
Upvotes: 0
Reputation: 6515
try like this
public static void main(String[] args) {
String str="a-695b-36c158d-666e-1326f144";
int aIndex=str.indexOf('a');
int bIndex=str.indexOf('b');
int cIndex=str.indexOf('c');
int dIndex=str.indexOf('d');
if(aIndex>-1 && bIndex>-1 && cIndex>-1&& dIndex>-1){
System.out.println("op1 -->"+str.substring(aIndex+1,bIndex));
System.out.println("op2 -->"+str.substring(bIndex+1,cIndex));
System.out.println("op3 -->"+str.substring(cIndex+1,dIndex));
}else{
System.out.println("invalid format");
}
}
Note :- Make sure the order of alphabets (a,b,c...) are in ascending order. IOBE occurs when the length of substring you are trying to access becomes -ve i.e (endIndex-beginIndex) becomes -ve.
Upvotes: 0
Reputation: 79857
You'll get that exception if the indexes of a
, b
, c
, d
are not in the expected order.
For example if the first index of a
is AFTER the first index of b
, then the expression str.substring(str.indexOf("a")+1, str.indexOf("b"))
will throw this exception, as described in the Javadoc for the substring
method, which promises ...
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex..
Upvotes: 1