Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java String to int

farenda 2016-10-28 0

In Java String to int conversion can be done in a few different ways. In this post we’ll show them and describe differences.

String to int

To create primitive int from String you can use Integer.parseInt(String):

int x = Integer.parseInt("42");
System.out.println("int is: " + x);

The above code produces the following output:

int is: 42

NumberFormatException when conversion fails

When Java cannot convert given String to int then NumberFormatException is thrown:

try {
    Integer.parseInt("aoeu");
} catch (NumberFormatException e) {
    e.printStackTrace();
}

The above code prints:

java.lang.NumberFormatException: For input string: "aoeu"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at com.farenda.java.lang.StringToInt.numberFormatException(StringToInt.java:18)
        at com.farenda.java.lang.StringToInt.main(StringToInt.java:7)

String to Integer

If you would like to convert String to Integer then there is Integer.valueOf(String) method exactly for that purpose:

Integer x = Integer.valueOf("42");
System.out.println("Integer is: " + x);

The code prints:

Integer is: 42

Note: It’s exactly like creating an Integer from result of Integer.parseInt(String).

NumberFormatException when String to Integer fails

Also here, when String to Integer conversion fails, the NumberFormatException is thrown:

try {
    Integer.valueOf("xyz");
} catch (NumberFormatException e) {
    e.printStackTrace();
}

Prints the following:

java.lang.NumberFormatException: For input string: "xyz"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.valueOf(Integer.java:766)

Integer from String in other base

Integer.decode(String) can be used to create Integer from octal, decimal, or hex String. Of course, this method may throw NumberFormatException as any other method that tries to create something from a String.

Octal String to Integer

System.out.println("From oct: " + Integer.decode("052"));

Prints:

From oct: 42

Decimal String to Integer

System.out.println("From dec: " + Integer.decode("42"));

Prints:

From dec: 42

Hex String to Integer

System.out.println("From hex: " + Integer.decode("0x2a"));

Prints:

From hex: 42

References:

  • Java int to String
  • Java Basics Tutorial
Share with the World!
Categories Java Tags java, java-basics
Previous: Java 8 reduce by example
Next: Java int to String

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored