# Thread: Help with type casting arrays

1. Member
Join Date
Sep 2010
Posts
56
Rep Power
0

## Help with type casting arrays

I am doing Project Euler, a mathematical challange for programmers. Problem 16 wants me to find the sum of the digits of the integer 2^1000. This program will find the number, but I am having trouble converting the character array to an integer array.

Java Code:
import java.math.BigInteger;
import java.lang.Integer;

class euler16
{
public static void main(String[] args)
{
BigInteger n = BigInteger.valueOf(2);
BigInteger two = BigInteger.valueOf(2);

boolean done = false;

int times = 0;
int num;
String c;

while(!done) //works out 2^1000
{
n = n.multiply(two);
times++;
if(times == 1000)
done = true;
}

String numbers = n.toString(); //Converts 2^1000 to a string

System.out.println(numbers);
System.out.println();

char[] numberArray = numbers.toCharArray(); //Converts the string to a character

for(int e = 0; e < numberArray.length; e++)
{
System.out.print(numberArray[e]);
}

System.out.println();

System.out.println();

int[] sumArray = new int[numberArray.length]; //Creates an integer array that is the same length as the character array

for(int e = 0; e < numberArray.length; e++) //Here lies the problem, this is supposed to convert the charcters to integers and put them into the int array
{
sumArray[e] = (int)(numberArray[e]);
}

for(int e = 0; e < sumArray.length; e++) //When the array is outputted, it is completely different
{
System.out.print(sumArray[e]);
}
}
}
The output of the program: 21430172143725346418968500981200036211228096234110 672148875007767407021022498722
44986396757631391716255189345835106293650374290571 384628087196915514939714960786
91355496484619708421492101247422837559083643060929 499671638825347975351183310878
92154125829142392955373084335320859663305248773674 411336138752

21430172143725346418968500981200036211228096234110 672148875007767407021022498722
44986396757631391716255189345835106293650374290571 384628087196915514939714960786
91355496484619708421492101247422837559083643060929 499671638825347975351183310878
92154125829142392955373084335320859663305248773674 411336138752

50495251484955504952515550535152545249565754565348 485756495048484851545049495050
56485754505152494948545550495256565553484855555455 524855485049485050525756555050
52525756545157545553555451495157495549545053534956 575152535651534948545057515453
48515552505748535549515652545056485655495754574953 534952575157554952575448555654
57495153535257545256525449575548565250495257504948 495052555250505651555353574856
51545251485448575057525757545549545156565053515255 575553515349495651514948565556
57504953524950535650574952505157505753535155514856 525151535150485653575454515148
53505256555551545552524949515154495156555350Press any key to continue . . .

Thanks!

2. Senior Member
Join Date
Apr 2010
Location
Philippines
Posts
580
Rep Power
4
What kind of trouble you are encountering in converting character to integer?
Have you tried Integer.parseInt?

3. Like stated by the poster above, if you use Integer.parseInt it is much simpler. Use the biginteger.toString() and think how you can use Integer.parseInt to solve this. Another thing, you don't need to import java.lang.Integer, all java.lang package items come standard and never need to be imported

Check out the methods for BigInteger, the loop you use to find 2^1000 is not needed because of a bigint method.

Finally, with this problem you can also create a method for finding digitsum which uses / and %, or, for BigInt .mod and .divide.

4. Member
Join Date
Sep 2010
Posts
56
Rep Power
0
Thanks for the help! I got it worked out.