converting 'char' to an integer

2 views
Skip to first unread message

Jeremy Watts

unread,
Aug 11, 2005, 11:08:45 AM8/11/05
to
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


Roedy Green

unread,
Aug 11, 2005, 11:27:10 AM8/11/05
to
On Thu, 11 Aug 2005 15:08:45 GMT, "Jeremy Watts"
<jwatt...@hotmail.com> wrote or quoted :

>
>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??

see http://mindprod.com/applets/converter.html

Thomas Fritsch

unread,
Aug 11, 2005, 11:27:31 AM8/11/05
to
Jeremy Watts wrote:
> 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

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

jan V

unread,
Aug 11, 2005, 11:43:48 AM8/11/05
to
> 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".


Joan

unread,
Aug 11, 2005, 12:45:46 PM8/11/05
to

"Jeremy Watts" <jwatt...@hotmail.com> wrote in message
news:1SJKe.5670$1F5....@newsfe4-win.ntli.net...
I like this method

int i = '5' - '0';

Raymond DeCampo

unread,
Aug 11, 2005, 1:56:08 PM8/11/05
to

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

--
XML is the programmer's duct tape.

Joan

unread,
Aug 11, 2005, 2:02:52 PM8/11/05
to

"Raymond DeCampo" <nos...@twcny.rr.com> wrote in message
news:YiMKe.7386$rI6....@twister.nyroc.rr.com...
Good point Ray. Can you give me an example where the set of
digits
is not contiguous?

Roedy Green

unread,
Aug 11, 2005, 2:21:27 PM8/11/05
to
On Thu, 11 Aug 2005 15:43:48 GMT, "jan V" <n...@nul.be> wrote or quoted
:

>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.

Raymond DeCampo

unread,
Aug 11, 2005, 3:35:54 PM8/11/05
to

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,

Patricia Shanahan

unread,
Aug 11, 2005, 4:19:19 PM8/11/05
to

Hexadecimal.

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

'a' - '0' is 49.

Patricia

jan V

unread,
Aug 11, 2005, 5:56:29 PM8/11/05
to
> >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.

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.

Joan

unread,
Aug 11, 2005, 6:37:23 PM8/11/05
to

"Patricia Shanahan" <pa...@acm.org> wrote in message
news:bpOKe.4422$WD....@newsread1.news.pas.earthlink.net...

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

Joan

unread,
Aug 11, 2005, 6:46:10 PM8/11/05
to

"Raymond DeCampo" <nos...@twcny.rr.com> wrote in message
news:uMNKe.7452$rI6...@twister.nyroc.rr.com...

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.

Raymond DeCampo

unread,
Aug 11, 2005, 8:54:14 PM8/11/05
to

That assumes you know which set you are dealing with. If that is the
case, then go for it.

>
>> 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.

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.

George Cherry

unread,
Aug 12, 2005, 12:09:15 AM8/12/05
to

"jan V" <n...@nul.be> wrote in message
news:hQPKe.167427$jp.91...@phobos.telenet-ops.be...

>> >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.
>
> 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/

Jeff Schwab

unread,
Aug 12, 2005, 12:20:25 AM8/12/05
to
>>>>> Jeremy Watts wrote:
>>>>>
>>>>>> 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?

>>>> Joan wrote:
>>>>
>>>>> I like this method
>>>>>
>>>>> int i = '5' - '0';

>>> Ray wrote:
>>>
>>>> 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';

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

> Patricia Shanahan wrote:
>
>> Hexadecimal.
>>
>> Character.digit('a',16) is 10.
>>
>> 'a' - '0' is 49.

Joan wrote:

> 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%. :)

jan V

unread,
Aug 12, 2005, 4:05:16 AM8/12/05
to

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.

Raymond DeCampo

unread,
Aug 12, 2005, 9:18:40 AM8/12/05
to
Jeff Schwab wrote:
>
> Joan wrote:
>
> > 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%. :)
>

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.

Oliver Wong

unread,
Aug 12, 2005, 4:00:02 PM8/12/05
to
"Joan" <Jo...@nospam.invalid> wrote in message
news:jbSdncRbdpp...@comcast.com...

> 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.


Jeff Schwab

unread,
Aug 12, 2005, 5:59:18 PM8/12/05
to

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.

George Cherry

unread,
Aug 12, 2005, 8:07:36 PM8/12/05
to

"jan V" <n...@nul.be> wrote in message
news:0LYKe.167617$q84.9...@phobos.telenet-ops.be...

>> Take a look at: http://www.pairprogramming.com/
>
> I'm a big fan of the concept... most people in management aren't.

That's because they don't--or can't--believe that pair programming
increases productivity.

> Typically
> you do what management tells you, or you're out of a job..

Or become a senior manager.

> I've yet to come
> across a company which truly embraces pair programming.

Interview management before you accept your next job offer.
: o )

Joan

unread,
Aug 15, 2005, 11:26:12 PM8/15/05
to

"Oliver Wong" <ow...@castortech.com> wrote in message
news:6d7Le.131045$wr.10453@clgrps12...
Can the OP provide more test cases pls?
If the set of inputs is small,you can just use a look up table
Or switch statement like this:

class A {
public static void main(String[] args) {
char jones = 'a';
int i;

switch (jones) {
default: System.err.println("you need a case
statement for " + jones);
System.exit(0);
case 'a' : i = 0;
break;
case 'b' : i = 1;
break;
// put rest of your cases here
}
System.out.println(jones);
System.out.println(i);
}
}

Reply all
Reply to author
Forward
0 new messages