This site requires JavaScript, please enable it in your browser!
Greenfoot back
nooby123
nooby123 wrote ...

2012/4/7

Char to integer

nooby123 nooby123

2012/4/7

#
I was wondering, is there any way to change "5" into 5?
danpost danpost

2012/4/7

#
I am sure there must be an easier way, but one way for a single digit is
"0123456789".indexOf("5");
sp33dy sp33dy

2012/4/7

#
Look at the API, specifically the Integer class: Javadocs Integer Look up the parseInt() method
Busch2207 Busch2207

2012/4/7

#
The following methods return 0, when the given char/String is no pure Number, else (of course) the given Number. :)
    public static int getInt(char ch)
    {
            return getInt(new String(new char[]{ch}));
    }
    
    public static int getInt(String T)
    {
        try
        {
            return Integer.parseInt(T);
        }
        catch(Exception e){return 0;}
    }
davmac davmac

2012/4/7

#
one way for a single digit is (...)
Another way is just: "5".charAt(0) - '0'
nooby123 nooby123

2012/4/7

#
Thanks very much!
You need to login to post a reply.