How do I convert string to an integer or number?
Category: java.lang, viewed: 9587 time(s).
package org.kodejava.example.lang;
public class StringToInteger {
public static void main(String[] args) {
// Some random selected number, could representing a decimal,
// hexadecimal or octal number.
String myLuckyNumber = "13";
// We convert a string to an integer by invoking parseInt() method
// of the Integer class.
Integer number = Integer.parseInt(myLuckyNumber);
System.out.println("My lucky number is: " + number);
// We can also converting a string representation of a number other
// then the decimal base, for instance an hexadecimal by providing
// the radix to the method.
number = Integer.parseInt(myLuckyNumber, 16);
System.out.println("My lucky number is: " + number);
number = Integer.parseInt(myLuckyNumber, 8);
System.out.println("My lucky number is: " + number);
}
}
Our code results are:
My lucky number is: 13
My lucky number is: 19
My lucky number is: 11
Can't find what you are looking for? Join our
FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!