Java Tutorials
HashMap in Java Learn with Example
What is Hashmap in Java? A HashMap basically designates unique keys to corresponding values that...
There are two ways to convert String to Integer in Java,
String strTest = “100”;Try to perform some arithmetic operation like divide by 4 – This immediately shows you a compilation error.
class StrConvert{ public static void main(String []args){ String strTest = "100"; System.out.println("Using String:" + (strTest/4)); } }
Output:
/StrConvert.java:4: error: bad operand types for binary operator '/' System.out.println("Using String:" + (strTest/4));
Hence, you need to convert a String to int before you peform numeric operations on it
int <IntVariableName> = Integer.parseInt(<StringVariableName>);
Pass the string variable as the argument.
This will convert the Java String to java Integer and store it into the specified integer variable.
Check the below code snippet-
class StrConvert{ public static void main(String []args){ String strTest = "100"; int iTest = Integer.parseInt(strTest); System.out.println("Actual String:"+ strTest); System.out.println("Converted to Int:" + iTest); //This will now show some arithmetic operation System.out.println("Arithmetic Operation on Int: " + (iTest/4)); } }
Output:
Actual String:100 Converted to Int:100 Arithmetic Operation on Int: 25
Integer.valueOf() Method is also used to convert String to Integer in Java.
Following is the code example shows the process of using Integer.valueOf() method:
public class StrConvert{ public static void main(String []args){ String strTest = "100"; //Convert the String to Integer using Integer.valueOf int iTest = Integer.valueOf(strTest); System.out.println("Actual String:"+ strTest); System.out.println("Converted to Int:" + iTest); //This will now show some arithmetic operation System.out.println("Arithmetic Operation on Int:" + (iTest/4)); } }
Output:
Actual String:100 Converted to Int:100 Arithmetic Operation on Int:25
NumberFormatException is thrown If you try to parse an invalid number string. For example, String ‘Guru99’ cannot be converted into Integer.
Example:
public class StrConvert{ public static void main(String []args){ String strTest = "Guru99"; int iTest = Integer.valueOf(strTest); System.out.println("Actual String:"+ strTest); System.out.println("Converted to Int:" + iTest); } }
Above example gives following exception in output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Guru99"
What is Hashmap in Java? A HashMap basically designates unique keys to corresponding values that...
Classes and Objects in Java are the fundamental components of OOP's. Often there is a confusion...
For-Each Loop is another form of for loop used to traverse the array. for-each loop reduces the...
What are Strings? A string in literal terms is a series of characters. Hey, did you say...
In this tutorial, we will learn- How to use Loop? Different Types of Loops for loop while loop...
What is Abstraction in Java? Abstraction in JAVA shows only the essential attributes and hides...