Link to home
Start Free TrialLog in
Avatar of pdidow
pdidow

asked on

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!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

int i = '0' - number;
Sorry!

int i = number - '0';
try this:
String strChar = String.valueOf(char);
int mynumber = Integer.parseInt(strChar);
Avatar of pdidow
pdidow

ASKER

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...?!??!?
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I mean:

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

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

pdidow - can you tell me why you accepted that particular answer?
Avatar of pdidow

ASKER

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.
 
>>
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
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)
>>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';
}