Results 1 to 7 of 7

Thread: valueOf()?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    valueOf()?

    Can someone please explain this
    to me. I want to get the integer
    or "numeric value" of a hex string.
    Now, i want to use the valueOf()method
    which is declared within the Integer
    wrapper class but you can only create
    a new instance of the Integer wrapper class
    with either:

    public Integer(String s);
    or
    public Integer(int value);

    the only way to use the valueOf() method is to first create a new Integer Object but you cannot create an Instance of Integer with out an initial value. You cant subclass Integer because it is final. So how do you use this method? I have no clue.

    class test{
    public static void main(String[] args){
    try{
    Integer hex = new Integer(????);
    String hexstring = "0xfffffff";
    long l = hex.valueOf(hexstring,16);
    System.out.println(l);
    }catch(Exception e){System.err.println(e);}
    }
    }

    this produces a java.lang.NumberFormatException
    so this dosent work either

    class test{
    public static void main(String[] args){
    try{
    Integer hex = new Integer("0xfffffff");
    System.out.println(hex.valueOf("0x7fffffff",16));
    }catch(Exception e){System.err.println(e);}
    }
    }

  2. #2
    VirtuallyVB
    Guest
    You don't need an instance to use those methods.

    int anInt = Integer.parseInt(aString);
    int anInt = Integer.parseInt(aString, aRadix);

    I never tried other numbering systems in Java, but I think this method will help you.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    yeah i dont know for some reason i thought i would have to create an instance of Integer to get to it's method. { It was a late night } I keep getting a incompatable type error though

    long l = Integer.valueOf("0xfffffff",16);
    ^
    found: java.lang.Integer
    required: int


    class test{
    public static void main(String[] args){
    try{

    long l = Integer.valueOf("0xfffffff",16);
    System.out.println(l);
    }catch(Exception e){System.err.println(e);}
    }
    }

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    this works:


    class test{
    public static void main(String[] args){
    try{
    int i = Integer.parseInt("7fffffff",16);
    System.out.println(i);
    }catch(Exception e){System.err.println(e);}
    }
    }

    but this fails to:


    class test{
    public static void main(String[] args){
    try{
    int i = Integer.valueOf("0x7fffffff",16);
    System.out.println(i);
    }catch(Exception e){System.err.println(e);}
    }
    }

  5. #5
    VirtuallyVB
    Guest
    You have to note the datatype being returned. Although the Integer class can return a long, you might prefer to use the Long class and its parseLong(String s, int radix) method.

  6. #6
    For some reason, the word "radix" makes me think that function is deprecated. You might want to check the JDK documentation before you use it.

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Trust me the function isnt depreacated.

    Integer intObj1 = Integer.valueOf(String s);

    returns a wrapper object corresponding to the to the primitive
    value represented by the String object passed as an arguement.

    What i was doing was using "Ox7fffffff"
    as an arguement which is what the decode() method takes.
    to use valueOf() or paseType() you have to omit the "Ox" or "O" or a NumberFormatException will be generated.

    these bolth work now:

    Code:
    class Test{ 
          public static void main(String[] args){ 
              try{ 
                Long l = Long.valueOf("7fffffff",16); 
                 System.out.println(l); 
              }catch(Exception e){System.err.println(e);} 
           } 
         }
    parseType(String s); returns a primative numeric value represented by a string object pass as an arguement.

    Code:
              class test{   
                 public static void main(String[] args){ 
                     try{ 
                       int i = Integer.parseInt("7fffffff",16); 
                        System.out.println(i); 
                    }catch(Exception e){System.err.println(e);} 
                  } 
                }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width