I can never get this to loop all the way through the string. I can’t use >= or == in this loop it stops it from working 🙁 help
int s = 0;
String twocharacters = "";
for (int i = 0; i < value.length(); i++){
char c = value.charAt(i);
char w = value.charAt(i + 1);
if (c == '/' && w == '*' && s == 0){
s = 1;
}
else if (c == '*' && w == '/' && s == 1){
s = 0;
}
else if (s == 0 && c != ' ' && c != '*' && c != '/'){
twocharacters += c;
System.out.println(twocharacters);
}
}
At the last iteration (
i = value.length() - 1), assigningvalue.charAt(i + 1);towwould throw an exception because you are at the end of the string and cannot get the next character.Try:
for (int i = 0; i < value.length() - 1; i++){