*
The moose likes Java in General and the fly likes addition of 2 characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "addition of 2 characters" Watch "addition of 2 characters" New topic
Author

addition of 2 characters

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Hi Friends,

Can you guide me on how to add 2 characters say a= '1' , b = '2' to yield a 3 (Not 99 ie 49 + 50; the ascii values)?

Thanks
Faisal
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
There is a constraint that this needs to be done without wrapper classes
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Is it that we need to traverse through all the ascii values, and lookup until we find a match ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So you have the character '1', which has a numerical value of 49, but you want to treat it as numerical value 1, and you have character '2', which has a numerical value of 50 but you want to treat it as a numerical value of 2?

So, you want the following mapping?



Yes? Look at the pattern above, and also consider the fact that you can do arithmetic on characters. Does that give you an idea of how to approach this?
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
I get the fact that I need to have a map.
I realise we can do arithmetic, but the results would be different.

char 'a' + 'b' = 99// not 3

Thanks
Faisal
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Jeff said mapping, not a Map. There is a much simpler way to do such arithmetic, using chars only.
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Hi Ritchie,

yes. I also meant mapping.

[There is a much simpler way to do such arithmetic, using chars only. ]
Could you give an example please ?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Faisal syed wrote:Hi Ritchie,

yes. I also meant mapping.

[There is a much simpler way to do such arithmetic, using chars only. ]
Could you give an example please ?

If you had the value x but you actually wanted the value 0, what would you take away from x to get 0.
If you had the value (x + 1) but you actually wanted the value 1, what would you take away from (x + 1) to get 1.
If you had the value (x + 2) but you actually wanted the value 2, what would you take away from (x + 2) to get 2.

Answer those questions and then hopefully the answer to your problem will become clear.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Faisal syed wrote: . . . yes. I also meant mapping. . . .
But you said map, which is different from mapping.
Could you give an example please ?
Stuart B has already given you as much information as you really need.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Campbell Ritchie wrote:
Faisal syed wrote: . . . yes. I also meant mapping. . . .
But you said map, which is different from mapping.
Could you give an example please ?
Stuart B has already given you as much information as you really need.


This may be a case where the OP should step back, take out a pencil and paper, and work it out first. Everyone here seems to easily envision what needs to be done, but I would guess that the reason for this, is that we have done it many many times before. Someone who has never done this should work it out on paper -- even though the maths behind it is ridiculously easy.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
I see what you mean, Henry. Sorry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: addition of 2 characters
 
Similar Threads
Why a legal identifier cannot include the character #
ASCII characters AND Java characters
web browser JTextField problem
Writing and reading string to/from a file
how can i set jdialog title to support Unicode languages