How to convert the given string to number in Java?
In this tutorial, i am going to explain how to convert a given String to Integer in Java with and without using predefined methods of Java. Input: "1234" Output: 1234 Input: "abc" Output: an Exception. Problem: How to convert the given string to number in Java? Method 1 - Using predefined method of Integer Class: use ParseInt() method of Integer class to convert given String to Integer. The code snippet is showed below. private static int convertToInteger2(String aString) { return Integer.parseInt(aString); } Method 2 - Without using predefined methods: In this approach, we are not going to use any predefined methods for conversion. we are going to write our own algorithm for this purpose. The algorithm is simple, we are going to iterate through each character of the give String and convert it to digit. we maintain a tempo...