• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

How to convert String to int using Java

February 15, 2017 by ProjectsGeek Leave a Comment

Convert String to int using Java

 

Using Integer.valueOf() in Java

This method uses valueOf() method to convert string into Integer object which we can use to print directly to console.

Source Code

package com.projectsgeek;

public class JavaExample {

/**
* @projectsgeek
*/
public static void main(String[] args) {

//Convert String to int using Java
String string = "99";
Integer StringResult = Integer.valueOf(string);
System.out.print(StringResult);
}
}

Output

Input value is 99 in form of string which will be converted to int using valueOf() method.

99

Integer.parseInt() Examples

This method uses parseInt() method for converting string into int in java.

package com.projectsgeek;

public class JavaExample {

/**
* @projectsgeek
*/
public static void main(String[] args) {

//Converting String to int using Java
String string = "99";
int stringResult = Integer.parseInt(string);
System.out.print(stringResult);
}
}

Output

Input value is 99 again and this string value will be converted to int using parseInt() method.

99

This method can convert any string value which is convertible to int. But what about values which you can’t convert to int for example “12d”,”aa” ? This java method will through exception when it is not able to convert it to int value.

package com.projectsgeek;

public class JavaExample {

/**
* @projectsgeek
*/
public static void main(String[] args) {

//Converting String to int using Java
String string = "99D";
int stringResult = Integer.parseInt(string);
System.out.println(stringResult);
}
}

Output

java.lang.NumberFormatException is thrown as parseInt() is not able to convert 99D value to int. 

Exception in thread "main" java.lang.NumberFormatException: For input string: "99D" [Passing non int value]
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.projectsgeek.JavaExample.main(JavaExample.java:12) [exception thrown while converting]

Reference 

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

Other Projects to Try:

  1. Implement using Socket Programming (TCP/UDP) in Java
  2. Developing Clock In java using Multi threading
  3. To Implement a Program Retrieval of Documents using Inverted Files
  4. To Perform various String Operation in Java
  5. How to convert Binary to Gray conversion

Filed Under: Java Tagged With: Java

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy