In order to convert a String to int in Java Programming language you can follow the below code examples,
- Using Interger.parseInt(String str) method
class StringToIntegerExample { public static void main(String[] args) { String stringInt = "25"; //A String variable holding an int value. int intValue = Integer.parseInt(stringInt); System.out.println(intValue); //Prints out 25 } }
But its always better to surround this code with a try-catch block as it can throw NumberFormatException if your String object is not holding an integer value, try replacing stringInt from 25 to A25, run the code and you will get the below error stack trace.
Exception in thread "main" java.lang.NumberFormatException: For input string: "A25" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at StringToIntegerExample.main(Client.java:6)
Let's put the above code in try/catch block:
class StringToIntegerExample { public static void main(String[] args) { String stringInt = "A25"; try { int intValue = Integer.parseInt(stringInt); System.out.println(intValue); } catch (Exception e) { System.out.println("Cannot convert " + stringInt + " to integer as not a number!"); } } }
- Using Integer.valueOf(String str) method
class StringToIntegerExample { /** * Example: * String to integer using * Integer.valueOf(String str) * * @param args */ public static void main(String[] args) { String stringInt = "300"; try { int intValue = Integer.valueOf(stringInt); System.out.println(intValue); } catch (Exception e) { System.out.println("Cannot convert " + stringInt + " to integer as not a number!"); } } }
More Posts related to Java,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- Android SecurityException: Need BLUETOOTH ADMIN permissicacheNameAndAddresson: Neither user 10123 nor current process has android.permission.BLUETOOTH_ADMIN - Android
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager - Android
- Read Text file from SD Card : Android Programming - Android
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext - Java
- How to know your Mac Screen Resolution? - MacOS
- BSNL Broadband upgrades speed to minimum 2MBps for all users 512Kbps 1Mbps - HowTos
- Android AlertDialog with 3 buttons example - Android
- How to Disable EditText Keyboard Android App - Android