How to Convert String to Int in Java - ParseInt Method

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java allows for many types of data type conversion. This lesson describes how to convert a String variable into an integer value using the ParseInt method in Java. Working code samples are provided.

Converting a String to an Int

Sometimes we need to work with numeric strings as if they are numbers. For example, a String object could hold an integer value, and we might need to do some work with it as an integer. For this task, we have the parseInt() function.

String q = new String("17");
int r = Integer.parseInt(q);
System.out.println(r);

The parseInt method can also accept a second parameter. It is called the radix and it indicates the number base of the value. If you have a string that is binary, octal or hexadecimal, you can get the decimal value of that number.

The base numbers are as follows:

  • Binary = 2
  • Octal = 8
  • Decimal = 10
  • Hexadecimal = 16

Let's take a look at a simple example. Hexadecimal numbers are base 16, and valid numbers start between 0 and F (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F). Therefore, the value of F should be 15. In order to indicate the hexadecimal number, we pass 16 to parseInt:

String q = new String("F");
int r = Integer.parseInt(q, 16);
System.out.println(r);

The output proves our assumption:


Java radix output - hex


If you've worked with HTML you've noticed that colors all have a hexadecimal value. For example, #FFFFFF is white. So, what would the integer value of the color 66ffcc be? Let's use parseInt to find out!

String q = new String("66ffcc");
int r = Integer.parseInt(q, 16);
System.out.println("The integer value of color " + q + " is " r);

The output of the code is:


Java parseint radix color output


Error-Checking

We are working with integers and Strings, what could go wrong? Actually, there are several things that could happen during the conversion. These can result in errors during the program run.

String myString = newString("hello");
int makeNumber = Integer.parseInt(myString);
System.out.println(makeNumber);

In order to catch the exceptions, place the parseInt function within a try and catch block. The code you want to execute is inside the try statement. After the catch statement you can display an error should the conversion fail. Since we are trying to convert a non-numeric number to numeric, the exception will be a NumberFormatException in the Java compiler.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support