Reputation: 9335
So I've looked at "questions that may already have my answer" but, despite getting the same error message as those questions, I think I may have a different issue:
//find greatest product generated by 5 consecutive integers below
class project_euler8 {
public static String numbers =
"73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
public static int calculateProduct(int[] myArray) {
int product = 1;
for (int i = 0; i < myArray.length; i++) {
product *= myArray[i];
}
return product;
}
public static void main(String[] args) {
//declare biggest_product, temporary array
int biggest_product = 0;
int[] temp = new int[5];
//loop through each sequence of 5 integers
for (int i = 0; i < numbers.length() - 5; i++) {
int remainder = i % 5;
**temp[remainder] = Integer.parseInt(numbers[i]);**
int candidate_product = calculateProduct(temp);
if (candidate_product > biggest_product) {
biggest_product = candidate_product;
}
}
System.out.println("Biggest product is " + biggest_product);
}
The line the compiler doesn't like is bolded above. If I declare my array (temp) within the for loop, will this fix the issue? I'm a bit confused why I can't assign integer values element-wise based on array index...
I know arrays in Java are immutable but, if this were the case, how could I assign values at any point after array declaration?
Upvotes: 0
Views: 2457
Reputation: 18320
It looks like you want to extract five digits from i
to i+5
. You can achieve this with substring method.
Instead of:
temp[remainder] = Integer.parseInt(numbers[i]);
do:
temp[remainder] = Integer.parseInt(numbers.substring(i,i+5));
Upvotes: 0
Reputation: 106389
numbers
isn't declared as an array; it is instead a String
.
If you wanted to iterate over each character in the String
, then that can be accomplished by numbers.charAt(i)
.
However, since what you're getting back is a char
and not a String
, you have to convert it appropriately (that is, subtract '0'
from your numerical character to normalize it).
Since a char
really is an int
(with a shorter range), there's no convenience method in Integer
to convert from a char
to an int
, so one has to subtract the char
'0'
to get a number back.
That solution would look something like this:
temp[remainder] = numbers.charAt(i) - '0';
This means that you have more work to do in changing the signature of your method that accepts an int[]
, but I leave that as an exercise for the reader.
Upvotes: 2