Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Conversion of character array to integer and viceversa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Conversion of character array to integer and viceversa" Watch "Conversion of character array to integer and viceversa" New topic
Author

Conversion of character array to integer and viceversa

Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
My problem is that,

1) I'm going to read two character arrays. These contain only numbers.

2) How can i add the numbers which are in those two character arrays?

3) And again, how can i store the sum result into another character array?


Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



1) I'm going to read two character arrays. These contain only numbers
2) How can i add the numbers which are in those two character arrays?



To be more simple, Use for loop to iterate through array ( assuming 2 array has same size) and do arthmetic operation (+ , - or watever) by using the index of the array , say int newCH = ch1[i] + ch2[i] ;

Note this does automatic casting to int and all arthmetic operation on prmitive types leads to only "int"

3) And again, how can i store the sum result into another character array?


For this , get the int newCH to cast to char to store in char array lile ch3[i]= (char) newCH. note an explicit cast is required for primitive types in JAVA.

Other option may be convert to Wrapper classes and let that take care of casting.
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
I already did like that,

ch1 has 1,2,3,4
ch2 has 9,8,8,8

for(i=0;i<=len;i++)
{
int n = ch1[i]+ch2[i];
ch[i] = (char)n;
}

But, I'm getting an exception as...... Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at int n = ch1[i]+ch2[i]; line


why so??
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



how do you calculate the length ? Array starts with "0" and ends with array.length -1 . So change the for loop to iterate only to length-1. try out yourselves
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
@Balu Sadhasivam
hey as you said....

if you add directly like... int n = ch1[i]+ch2[i];

The output will be addition of ASCII values of numbers... so, you have to subtract the value of 0 to get the addition result of the numbers.

int n = ch1[i]-48 + ch2[i]-48;
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Chan ,

char always assume the ASCII value if its surronded by sinlge quotes say '0' , otherwise if not specified it assumes as number ( and no need of -48 )

and when casting int to char , it assumes as ASCII value and certainly you cannot get desired output . char is not good agent when to use numbers , use short instead.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

The common idiom is to have your loop run to < arrray.len

>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
Balu Sadhasivam wrote:
Chan ,

char always assume the ASCII value if its surronded by sinlge quotes say '0' , otherwise if not specified it assumes as number ( and no need of -48 )

and when casting int to char , it assumes as ASCII value and certainly you cannot get desired output . char is not good agent when to use numbers , use short instead.


Yep, I can use short instead of char. But, I need to hold more than 2000 numbers in a digit. That's why I wanna use char. Is there any other solution with you to hold such a large digit?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Yep, I can use short instead of char. But, I need to hold more than 2000 numbers in a digit. That's why I wanna use char. Is there any other solution with you to hold such a large digit?



What stops you to make short to hold 2000 ? And as already said , when dealing with integers , use short or integers. thats the end of the story.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion of character array to integer and viceversa
 
Similar Threads
An object to hold several arrays
algoritem writing problem
sum of two int arrays
Arrays
Trouble with compiling?