Results 1 to 4 of 4
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default 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. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What kind of trouble you are encountering in converting character to integer?
    Have you tried Integer.parseInt?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #4
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

Similar Threads

  1. Type casting
    By kiros88 in forum New To Java
    Replies: 17
    Last Post: 09-02-2010, 01:58 PM
  2. Type Casting
    By Shaheen Mohamed in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 07:56 PM
  3. help with type casting.
    By ramsrocker in forum Java Applets
    Replies: 15
    Last Post: 02-26-2009, 11:28 PM
  4. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 07:07 PM
  5. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •