converting 'char' to an integer

J

Jeremy Watts

Hi,

Sorry for some of these newbie questions ive been posting, but i really cant
get this to work. i have a Java 'char' (it will always be a number) defined
but want to convert it to an integer. How do I do this??

I have tried using 'digit()' and 'getNumericValue()' but seem to always get
an error message. Am I not importing the right class here? I've so far
imported java.lang.Character. Maybe I am misusing the statements. How
would you ordinarily do something like this?


Thanks
 
T

Thomas Fritsch

Jeremy said:
Sorry for some of these newbie questions ive been posting, but i really cant
get this to work. i have a Java 'char' (it will always be a number) defined
but want to convert it to an integer. How do I do this??

I have tried using 'digit()' and 'getNumericValue()' but seem to always get
an error message. Am I not importing the right class here? I've so far
imported java.lang.Character. Maybe I am misusing the statements.
How exactly did you try to use it? Did you get compiler errors? And if
so, what errors did you get?
How would you ordinarily do something like this?
Because the two methods mentioned are static, you have to write the
calls prefixed with the class name. May be this is what you missed(?)

char c = '3';
int i = Character.digit(c, 10); // gives i = 3
int j = Character.getNumericValue(c); // gives j = 3
 
J

jan V

Sorry for some of these newbie questions ive been posting,

You should feel ashamed, not sorry. With your embryonic level of knowledge
of Java, what you should be doing instead of posting here is reading some
introductory books on Java. There's no excuse not to, since there are a
number of totally free texts available online. For example "Thinking in
Java".
 
J

Joan

Jeremy Watts said:
Hi,

Sorry for some of these newbie questions ive been posting, but
i really cant
get this to work. i have a Java 'char' (it will always be a
number) defined
but want to convert it to an integer. How do I do this??

I have tried using 'digit()' and 'getNumericValue()' but seem
to always get
an error message. Am I not importing the right class here?
I've so far
imported java.lang.Character. Maybe I am misusing the
statements. How
would you ordinarily do something like this?
I like this method

int i = '5' - '0';
 
R

Raymond DeCampo

Joan said:
I like this method

int i = '5' - '0';

I think the problems with that method include that other locales will
have different characters that represent digits and it will fail for
those locales.

If you don't care about internationalization or you know that you have
one of the Arabic numerals 0 through 9 then go for it.

Although, again, I will say that your example would be improved by
including variables. E.g.

char ch = getSomeChar();
int i = ch - '0';

Ray
 
J

Joan

Raymond DeCampo said:
I think the problems with that method include that other
locales will have different characters that represent digits
and it will fail for those locales.

If you don't care about internationalization or you know that
you have one of the Arabic numerals 0 through 9 then go for it.

Although, again, I will say that your example would be improved
by including variables. E.g.

char ch = getSomeChar();
int i = ch - '0';

Ray
Good point Ray. Can you give me an example where the set of
digits
is not contiguous?
 
R

Roedy Green

You should feel ashamed, not sorry. With your embryonic level of knowledge
of Java, what you should be doing instead of posting here is reading some
introductory books on Java. There's no excuse not to, since there are a
number of totally free texts available online. For example "Thinking in
Java".

Only when people persist after being given such information are they
deserving of scorn. Everyone started out knowing nothing, even you.
 
R

Raymond DeCampo

Joan said:
Good point Ray. Can you give me an example where the set of digits
is not contiguous?

I'm not sure what you are asking. Are you looking for a locale where
the set of digits for that locale are not contiguous? I'm not aware of
any, but then I'm only aware of my locale's digits so I'm not the one to
ask.

If you are curious about what these characters might be, you can run the
following program to find all the characters that Java considers digits
and look them up at http://www.unicode.org:

public class CharDigits
{
public static void main(String args[])
{
for (int ch = Character.MIN_VALUE;
ch < Character.MAX_VALUE;
ch++)
{
if (Character.isDigit(ch))
{
final String hex = "000" + Integer.toHexString(ch);
final int len = hex.length();
System.out.println("\\u" + hex.substring(len - 4, len));
}
}
}
}

HTH,
Ray
 
P

Patricia Shanahan

Joan said:
Good point Ray. Can you give me an example where the set of digits
is not contiguous?

Hexadecimal.

Character.digit('a',16) is 10.

'a' - '0' is 49.

Patricia
 
J

jan V

You should feel ashamed, not sorry. With your embryonic level of
knowledge
Only when people persist after being given such information are they
deserving of scorn. Everyone started out knowing nothing, even you.

I was taught by some tough teachers... and, with hindsight, their teaching
was pure pedagogic magic. I guess I'm just passing on their style. Life, as
your fantastic website clearly acknowledges, is a tough old son of bitch
itself, so it's just fitting that students are hardened and forewarned early
rather than late. Programming, as many stars of our profession have
highlighted over the years, is an essentially lonely intellectual pursuit.
That means good programmers need to be able to knuckle down and concentrate,
real hard... and analyse, and try things out, and fail, and try again, and
fail and try again.

If the OP doesn't learn soon to go head for the nearest bookshelf or website
and READ, he'll never become even a mediocre programmer, so I'd say I'm
doing him a big favour showing him early what it takes to be in this game,
lest he spends an inordinate amount of time wasting his time.
 
J

Joan

Patricia Shanahan said:
Hexadecimal.

Character.digit('a',16) is 10.

'a' - '0' is 49.

Patricia

Sorry, I don't get what you are driving at/to. The OP says his
characters
will always be digits.
 
J

Joan

Raymond DeCampo said:
I'm not sure what you are asking. Are you looking for a locale
where the set of digits for that locale are not contiguous?
I'm not aware of any, but then I'm only aware of my locale's
digits so I'm not the one to ask.

My point is that you can subtract the value of the lowest digit
in the set
of digits from the digit you are testing to obtain a numeric
integer result,
unless the characters are not consecutive.
If you are curious about what these characters might be, you
can run the following program to find all the characters that
Java considers digits and look them up at
http://www.unicode.org:
<snip>
Cute program, there are 208 of them.

BTW: My book says that the Han (Chinese) ideographs occupy 21
bits and
cannot be represented in a single char value.
 
R

Raymond DeCampo

Joan said:
My point is that you can subtract the value of the lowest digit in the set
of digits from the digit you are testing to obtain a numeric integer
result,
unless the characters are not consecutive.

That assumes you know which set you are dealing with. If that is the
case, then go for it.
<snip>
Cute program, there are 208 of them.

BTW: My book says that the Han (Chinese) ideographs occupy 21 bits and
cannot be represented in a single char value.

I've sure that is true, I'm no Unicode expert. I'm not sure how Java
handles those kinds of things relative to the Character.isDigit() method.

HTH,
Ray
 
G

George Cherry

jan V said:
I was taught by some tough teachers... and, with hindsight, their teaching
was pure pedagogic magic. I guess I'm just passing on their style. Life,
as
your fantastic website clearly acknowledges, is a tough old son of bitch
itself, so it's just fitting that students are hardened and forewarned
early
rather than late. Programming, as many stars of our profession have
highlighted over the years, is an essentially lonely intellectual pursuit.

Take a look at:
http://www.pairprogramming.com/
 
J

Jeff Schwab

Sorry, I don't get what you are driving at/to. The OP says his
characters will always be digits.

Right, but digits in what base? 'a' is a valid digit in hexadecimal.

Since the OP almost certainly meant only '0' thru '9', I still give
Ray at least a 10/16.0 = 62.5%. :)
 
J

jan V

Take a look at: http://www.pairprogramming.com/

I'm a big fan of the concept... most people in management aren't. Typically
you do what management tells you, or you're out of a job.. I've yet to come
across a company which truly embraces pair programming.
 
R

Raymond DeCampo

Jeff said:
Right, but digits in what base? 'a' is a valid digit in hexadecimal.

Since the OP almost certainly meant only '0' thru '9', I still give
Ray at least a 10/16.0 = 62.5%. :)

I'm not sure what you mean. My point was that the ch - '0' trick won't
won't in an internationalized program. It had nothing to do with
hexadecimal digits.

Ray
 
O

Oliver Wong

Joan said:
Good point Ray. Can you give me an example where the set of digits
is not contiguous?

Japanese. The character representing the concept of 1 (ichi) has unicode
codepoint U+4e00. The character representing the concept of 2 (ni) has
unicode codepoint U+4e8c. For 3 (san), it's U+4e09. You can see the chart at
http://www.unicode.org/charts/PDF/U4E00.pdf.

If there's a pattern, I haven't figured it out (it's certainly not
ordered by stroke count, for example), but then again I'm not a native
speaker of Japanese. I'll have to ask some Japanese friends if they can
figure out the rationale for this ordering.

- Oliver.
 
J

Jeff Schwab

Raymond said:
I'm not sure what you mean. My point was that the ch - '0' trick won't
won't in an internationalized program. It had nothing to do with
hexadecimal digits.

Joan asked for a set of contiguous digits with non-contiguous character
values. Pat provided such a set. It's not what you were driving at,
but it does provide a tangible example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top