下面这两句为什么会抛出异常呢?
String str="0x00ffff00";
try
{
  int rgb=Integer.parseInt(str);
}
catch(Exception e)
{
  System.out.println("error "+e.getmessage());
}
执行的时候就会出现异常~~

解决方案 »

  1.   

    parseInt支持十进制,你这个"0x00ffff00"是十六进制
      

  2.   

    String sStr="0x00ffff00";
    Integer i=null;
    try{
    i=Integer.decode(sStr);
    }catch(NumberFormatException e){
    e.printStackTrace(System.err);
    }
    System.out.println("pring i="+i.intValue());
      

  3.   

    int rgb=Integer.parseInt(str,16);
      

  4.   

    Examples:  parseInt("0", 10) returns 0
     parseInt("473", 10) returns 473
     parseInt("-0", 10) returns 0
     parseInt("-FF", 16) returns -255
     parseInt("1100110", 2) returns 102
     parseInt("2147483647", 10) returns 2147483647
     parseInt("-2147483648", 10) returns -2147483648
     parseInt("2147483648", 10) throws a NumberFormatException
     parseInt("99", 8) throws a NumberFormatException
     parseInt("Kona", 10) throws a NumberFormatException
     parseInt("Kona", 27) returns 411787
      

  5.   

    public static int parseInt(String s,int radix)
    String str="00ffff00"
    Integer.parseInt(str,16);
      

  6.   

    public static int parseInt(String s,
                               int radix)
                        throws NumberFormatExceptionParses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned. 
    An exception of type NumberFormatException is thrown if any of the following situations occurs: The first argument is null or is a string of length zero. 
    The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX. 
    Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1. 
    The value represented by the string is not a value of type int.
      

  7.   

    int rgb=Integer.parseInt(str,16);
      

  8.   

    int rgb=Integer.parseInt("0x00ffff00",16);
    好象还是抛异常
      

  9.   

    楼上
    不是"0x00ffff00",而是"00ffff00"
    后面已经有个16可以表征进制了
      

  10.   

    to  Foxman110(野狼)Integer.decode("0x00ffff00").intValue()
    的确也可以实现,但是decode()调用valueOf(),valueOf()调用parseInt(),所以你那样做是不是舍近求远啊?呵呵。感觉能养成读源码的习惯很不错
      

  11.   

    int rgb=Integer.parseInt(str,16);
      

  12.   

    为什么要转换呢,我在想,难道0x00ffff00就不是int 了
    int i=0x00ffff00;
    这样有什么问题么?
      

  13.   

    我同意wizardblue(不死鱼)观点
    为什么要转换,0x00ffff00就不是int了吗?
      

  14.   

    关键看这个0x00ffff00怎么来的,楼主这么问当然知道这个是int了,假如是用一个JOptionPane.showInputDialog输入的,怎么赋值啊,还不是要parseInt
      

  15.   

    感谢各位~~但是,还是有问题~~
    1 J2ME没有Integer.decode()这个函数
    2 用Integer.parseInt(str,16)还是抛出异常。
    我要的str转成16进制必须以"0x"开头,要不然图像不能显示。呵呵~~大家再费费心帮我想想呗~~
    谢谢
    对了,现在还能增分吗?呵呵~~我可以加分啊~~