In the code below, inside the inner for loop, I get the following error:
LargestProdInSeries.java:16: charAt(int) in java.lang.String cannot be applied to (java.lang.Long)
String c = numb.charAt(j);
Why should this happen when I have declared num to be a String?
Does java do an auto conversion seeing that my entire string is a number? How do I prevent this?
public class LargestProdInSeries {
public static void main(String[] args) {
String numb = "731671";
Long maxProd=0L;
for(Long i = 0L; i < numb.length() ; i++) {
Long prod=1L;
for(Long j=i ; j<i+3 ; j++) {
String c = numb.charAt(j);
prod *= Long.parseLong(c);
}
//inner for
if(maxProd<prod) {
System.out.println(maxProd);
}
}
System.out.println("Max Prod = "+maxProd);
}
}