This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

easier way to tell whether or not an object is an int or char...  RSS feed

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crap...

im trying to switch a character, and see if it's really a number

or an operator (for an expression evaluation assignment).

is there any easier way to do this, instead of switch statement??

thanks,


Justin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Character.isDigit()?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, but i just made two char arrays, one containing Characters of the

digits from 0-9, and another char array with Characters of the operators,

+,-,/,*,^

and then i just looped through those arrays and compared to the character

passed throught the method



thanks again,

Justin
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Using arrays is not very nice looking. Maybe you should try to use Stan's way, or check for (c > '0' && c < '9').

2.

can be replaced by return(is)

3. You can use java.util.Arrays class to perform some search in arrays.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:




Have you read the API for getNumericValue? It's first paragraph is:

Returns the int value that the specified character (Unicode code point) represents. For example, the character '\u216C' (the Roman numeral fifty) will return an int with a value of 50.

Hmmm... so you want to be able to handle Roman numerals?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well since you want to be a smart guy...

try it, getNumericValue will work for digits from 0-9 atleast...

why did you reply if you didnt try it yourself first??

and besides, where in the above posts did i mention anything of

roman numerals???

did you not know that every character has a numerical value???

E.g
numeric value for the character '+' is -1

I'm trying to get help, and all you can do is try to tell me im dumb?

this site needs to make a developmental beginner forum for

idiots like yourself


Justin
[ August 29, 2006: Message edited by: Justin Fox ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
well since you want to be a smart guy...

try it, getNumericValue will work for digits from 0-9 atleast...

why did you reply if you didnt try it yourself first??

and besides, where in the above posts did i mention anything of

roman numerals???

did you not know that every character has a numerical value???

E.g
numeric value for the character '+' is -1

I'm trying to get help, and all you can do is try to tell me im dumb?

this site needs to make a developmental beginner forum for

idiots like yourself


Justin

[ August 29, 2006: Message edited by: Justin Fox ]


Dude, that post was way out of line. They were just trying to inform you that the method might not do what you think it does for the full range of possible parameters. No one was trying to insult you they were trying to help you. If you are that quick to insult somone who might help you you may find that people are going to be a lot less willing to put forth effort on your behalf in the future.
[ August 29, 2006: Message edited by: Garrett Rowe ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
why did you reply if you didnt try it yourself first??


Probably to help you, I'd guess.


I'm trying to get help, and all you can do is try to tell me im dumb?


No one was telling you that, as far as I can tell. Dana had a doubt and posted about it. It might have been an unfounded doubt, but I don't see anything that hints at malicious intents.


this site needs to make a developmental beginner forum for idiots like yourself


Watch your language. Calling people names will not be tolerated.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, I know, as well as you, that Character.getNumericValue(char a);

works for digits 0-9....

thats the only "numbers" i am using...

not once did i even hit towards wanting to use Roman Numerals..

so the quote: "hmmm...so you want to be able to handle Roman Numerals"

indeed, was intended to be a suttle insult, saying that I had no idea

what Character.getNumericValue(char a); is used for.

Justin
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what people are reacting to is that your method toInt() can be called on any character, and so could give some meaningless answers. Sometimes it might be appropriate to have the method throw an exception if a character is not a digit; othertimes not. You haven't showed us how you're using that method, so we can't really say -- maybe you'd only use it after testing if a character is a digit.

Now, I'm not clear on why you're not using Character.isDigit(), as suggested, and if I had to write your anOp() method, I think I'd probably write it like this:

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats pretty nifty...


the whole returning whether or not the operator is an index..
never would've thought of that..

for the .isDigit(); i am using that now..

the reason im not posting code is because im at school, and my files
are one the computer at my house.

ok... so ill explain best i can..

i have a class called Converter

and in it, it has the boolean methods isInt() and isOp()

also the actual converting methods toInt() and toChar()

I want to make my application to where when i run the main class,

it only calls one function, and that method does all the output to the monitor, and evaluation of the expression..

which i have already done...

now if i wanted to call class Converter's methods, all i have to do is

Converter is = new Convertor(); // right?

and then i can call it's methods like so..



Justin

[ August 30, 2006: Message edited by: Justin Fox ]

[ August 30, 2006: Message edited by: Justin Fox ]
[ August 30, 2006: Message edited by: Justin Fox ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!