xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to convert numeric string data to integer, float or double type in Java
Java Online Editor
Execute Code
More Java Sample Codes
import java.math.BigDecimal; public class ExampleApp { public static void main(String[] args) { // Convert string to integer int outputInteger; String stringVariable = "1225"; // this string data will be converted as it has number digits try { outputInteger = Integer.parseInt(stringVariable); System.out.println(outputInteger + 25); // 25 added to the integer. Output will be 1250 } catch (NumberFormatException e) { System.out.println(stringVariable + " cannot be converted to integer."); } // Convert string to float float outputFloat; String stringVariableNum = "75.25"; // this string data will be converted to a float try { outputFloat = Float.parseFloat(stringVariableNum); System.out.println(outputFloat + 12.46f); // 12.46 added, output will be 87.71 } catch (NumberFormatException e) { System.out.println(stringVariableNum + " cannot be converted to float."); } // Convert string to double double outputDouble; String stringNewVariable = "1225.25"; // this string data will be converted to a double try { outputDouble = Double.parseDouble(stringNewVariable); System.out.println(outputDouble + 12.46); // 12.46 added, output will be 1237.71 } catch (NumberFormatException e) { System.out.println(stringNewVariable + " cannot be converted to double."); } // Another example with non-numeric string int outputInteger2; String stringVariable2 = "T1225"; // this string data will not be converted as it contains non-numeric characters try { outputInteger2 = Integer.parseInt(stringVariable2); System.out.println(outputInteger2 + 25); } catch (NumberFormatException e) { System.out.println(stringVariable2 + " cannot be converted to integer."); } } }