Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion of character array to integer and viceversa

 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The common idiom is to have your loop run to < arrray.len

>
 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic