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
>
>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??
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('$','@')
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".
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
--
XML is the programmer's duct tape.
>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'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,
Hexadecimal.
Character.digit('a',16) is 10.
'a' - '0' is 49.
Patricia
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.
Sorry, I don't get what you are driving at/to. The OP says his
characters
will always be digits.
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.
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.
Take a look at:
http://www.pairprogramming.com/
>>>> 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%. :)
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.
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.
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.
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.
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 )
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);
}
}