Home > Tutorial > Java String To Integer Sample Code

Java String To Integer Sample Code

The Java Integer class has a nice method called parseInt() that will take a String and convert it to an Integer. This is very helpful when you need to store values as an integer that are defined as a String.

Java String To Integer Source Code:

package com.jcd.java.tutorials;

import java.util.Properties;

public class JavaStringToInteger 
{

	public static void main(String[] args) 
	{
		
		//Create properties table
		Properties properties = new Properties();
		
		//Set a local property using string as the value
		properties.setProperty("INTERVAL_RETRIES", "100000");
		
		//Store property as a String
		String intervalRetriesString = properties.getProperty("INTERVAL_RETRIES");
		
		System.out.println("\nINTERVAL RETRIES AS STRING: "+intervalRetriesString);
		
		//Convert the string to an integer 
		int intervalRetriesInteger = Integer.parseInt(properties.getProperty("INTERVAL_RETRIES"));
		
		System.out.println("\nINTERVAL RETRIES AS INTEGER: "+intervalRetriesInteger);

	}

}



Sample Java String To Integer Output:


INTERVAL RETRIES AS STRING: 100000

INTERVAL RETRIES AS INTEGER: 100000



References:

Smedley
Posted by

Smedley

Smedley has been doing software development for years, which mainly docues on C and C++. In the past number of years, he has focused on Java programming.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot