Write a Java Program to Convert String to Int

January 25, 2023 0 Comments

In this tutorial learn how you can write a program to convert String to int in java. Here we are using a class Integer.parseInt() and we will import “import java.lang.Integer;” and Scanner by “import java.util.Scanner;”

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Write a Program to convert String to Int in Java

Here is the code to convert String to int in Java programming.

//Write a program to convert String to int. Hint Integer.parseInt()
import java.lang.Integer;
import java.util.Scanner;

public class Convert {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter any number for string:");
        String word = input.nextLine();
        int number = Integer.parseInt(word);
        System.out.println("String converted to int: " + number + ".");
        input.close();
    }
}

Test Live At Jdoodle

Conclusion

This is how you can write a program to convert String to int in java programming. Comment below if you like our tutorial.

Leave a comment

Gallery