Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert char to int

Posted on 2004-11-13
14
Medium Priority
?
436 Views
Last Modified: 2010-03-31
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!
0
Comment
Question by:pdidow
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12575031
int i = '0' - number;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12575040
Sorry!

int i = number - '0';
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12575042
try this:
String strChar = String.valueOf(char);
int mynumber = Integer.parseInt(strChar);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pdidow
ID: 12575090
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
 
LVL 13

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12575092
correction:
    if (type.equals("GetRandomNumberCount") && mynumber >= 4 &&  mynumber <=10)
     {  
     ............
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12575229
I mean:

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12575566
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
 
LVL 2

Expert Comment

by:kjayaraman
ID: 12577783
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12578208
>>
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12581832
pdidow - can you tell me why you accepted that particular answer?
0
 

Author Comment

by:pdidow
ID: 12595891
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12596365
>>
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12596405
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12596457
>>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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question