You can convert strings into numbers using the Integer, Float, Double and Long type wrapper classes as indicated by the following code snippet:

 

class ConvertTest {

  public static void main (String args[]) {

    String str;
  
    str = "25";
  
    int i = Integer.valueOf(str).intValue();
    System.out.println(i);
    long l = Long.valueOf(str).longValue();
    System.out.println(l);

    str = "25.6";
  
    float f = Float.valueOf(str).floatValue();
    System.out.println(f);
    double d = Double.valueOf(str).doubleValue();  
    System.out.println(d); 
  
  }
  
}

There are no equivalent Short and Byte classes in Java 1.0. There are in Java 1.1. For shorts and bytes use the Integer class but use the byteValue() or shortValue() methods instead.

Posted in: Java

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677