, asked
Views : 4

How to convert a Java String to an Int?

Can anyone explain with an example how to convert a Java String to an integer?

1 Answer
Alankritha Ammu

The parseXxx() method is used to get the primitive data type of a certain String. In this case to convert a String to integer you could use the parseInt() method.

Example:

public class Test {
   public static void main(String args[]) {
      int x =Integer.parseInt("9");
      double c = Double.parseDouble("5");
      int b = Integer.parseInt("444",16);
      System.out.println(x);
      System.out.println(c);
      System.out.println(b);
   }
}

Output:

9
5.0
1092

Advertisements
Related Questions