I am trying to find a character array inside of a character array so that in the array, A would be followed by B. For some reason, I do not get an error but I also cannot get it to do the work I want when I have an array with the combination. I was just wondering if someone could explain what I am doing wrong and point me in the right direction.
char[] Array = new char[] {'A','B','C','D'....};
char A = 'A';
char B = 'B';
....
for (int i = 0 ; i < Array.length; i ++) {
if (Array[i] == A && Array[i++] == B) {
//Work Here
}
}
First, ont to make i + 1
exceed array indexes, use i < arr.length - 1
in for loop. Then, change arr[i++]
to arr[i+1]
in if
statement.
char[] arr = new char[] {'A','B','C','D'....};
char a = 'A';
char b = 'B';
....
for (int i = 0 ; i < arr.length - 1; i++) {
if (arr[i] == a && arr[i+1] == b) {
//Work Here
}
}
P.S: prop names are switched from capital case on purpose, to be consistent with the Java naming conventions.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments