Convert char to int

This seems very simple, but help!

the code:

private static boolean GetVaildNumber(char number,String type) throws Exception
{
      boolean bValid=false;
      //convert char number to int
      int mynumber=(new Integer(number)).intValue();
            
System.out.println("mynumber:" + mynumber);

}


when the new number is printed, for example a 5 comes out 53, a 7 is 55...etc....the decimal value on the asciii table...I want the number to be an int! 5 =5 and all!
pdidowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
int i = '0' - number;
0
CEHJCommented:
Sorry!

int i = number - '0';
0
petmagdyCommented:
try this:
String strChar = String.valueOf(char);
int mynumber = Integer.parseInt(strChar);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

pdidowAuthor Commented:
private static boolean GetVaildNumber(char number,String type) throws Exception
{
      boolean bValid=false;
      //convert char number to int

      String strChar = String.valueOf(number);
      int mynumber = Integer.parseInt(strChar);
      
      if (type == "GetRandomNumberCount" && mynumber >= 4 &&  mynumber <=10)
      {  
            //test for a integer value between 4-10
                  
                  
                  bValid=true;
                  
            
      }         
      

returns false for some reason...?!??!?
0
petmagdyCommented:
correction:
    if (type.equals("GetRandomNumberCount") && mynumber >= 4 &&  mynumber <=10)
     {  
     ............
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
I mean:

>>     if (type == "GetRandomNumberCount" && mynumber >= 4 &&  mynumber <=10)
should be:
    if (type.equals("GetRandomNumberCount") && mynumber >= 4 &&  mynumber <=10)

0
CEHJCommented:
I'm not sure why you ignored my previously posted shorter answer, but if that's what you want to do, there's no need for a conversion:

return "GetRandomNumberCount".equals(type) && mynumber >= '4' &&  mynumber <= '9';

The following test

>>mynumber <=10

cannot be correct since a single char cannot represent 10 unless it has *not* been converted in the way you want
0
kjayaramanCommented:
CEHJ,

I loved your solution, the first one that was simple
int i = '0' - number;

On second thought, just consider a scenario where the character is not between '0' and '9'. This would give you negative integers or some integer greater than 9... Ideal scenario is to throw an exception in such a case. Integer.parseInt would handle that. [Explicitly checking if the integer is between 0 and 9 solves the problem.]

And your solution

return "GetRandomNumberCount".equals(type) && mynumber >= '4' &&  mynumber <= '9';

is one of a kind. Great going mate.
0
CEHJCommented:
>>
I loved your solution, the first one that was simple
int i = '0' - number;
>>

Thanks, although that particular one of course is wrong ;-) (see my next comment after that one)

>>This would give you negative integers or some integer greater than 9

That's true, but of course i was expecting (before further posts by pdidow showed me what s/he was *actually* intending) the resulting int to be used in a range check, so that would not have been a problem.

As it happens, as my last comment shows, no conversion is necessary anyway

0
CEHJCommented:
pdidow - can you tell me why you accepted that particular answer?
0
pdidowAuthor Commented:
CEHJ:

Totally right about the char....but i my main problem was

 if (type == "GetRandomNumberCount" && mynumber >= 4 &&  mynumber <=10)
     {

instead of

 if (type.equals("GetRandomNumberCount") && mynumber >= 4 &&  mynumber <=10)
     {  

thanks for the help though...I have not worked with java much so some of my questions may be very beginnerish.
 
0
CEHJCommented:
>>
instead of

 if (type.equals("GetRandomNumberCount") && mynumber >= 4 &&  mynumber <=10)
>>

Two points:

a. That uses a conversion of the char to an int, which my code shows is unnecessary
b. It will fail if you pass it a null String - mine won't
0
petmagdyCommented:
offcourse i don't understand (a) because mynumber is int
b- to avoid null value case let it be:
 if ("GetRandomNumberCount".equals(type) && mynumber >= 4 &&  mynumber <=10)
0
CEHJCommented:
>>offcourse i don't understand (a)

The title of the question:

>>Convert char to int

(Unnecessary)

The answer:

private static boolean GetVaildNumber(char number,String type)
{
      return "GetRandomNumberCount".equals(type) && mynumber >= '4' &&  mynumber <= '9';
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.