-
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. :confused:
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 :mad:
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);}
}
}
-
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.
-
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 :p } 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);}
}
}
-
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);}
}
}
-
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.
-
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.
-
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. :rolleyes:
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);}
}
}