I have two type of implementations for processing. Not sure why stream forEach loop complains that variable 'i' must be final. But if final, then how to address issue with logic of computing 'i'?
public static void main(String[] args) {
String str = "123456789";
int i = 0;
//Non Stream
for (char c : str.toCharArray()) {
if (c >= 48 && c <= 57) {
i = i * 10 + (c - 48);
}
}
System.out.println(i);
i = 0;
// WHY compiler fails for variable 'i' here?
str.chars().forEach((c) -> {
if (c >= 48 && c <= 57) {
i = i * 10 + (c - 48);
}
});
System.out.println(i);
}
48and57instead of'0'and'9'…