Contents

Convert a string to an integer (with.Java)

   Nov 10, 2023     0 min read

This is a recap of the ā€œConvert a string to an integerā€ problem.

We’re going to learn by solving coding test questions, reflecting on the problems we solved, and exploring other ways to solve them.

Let’s start with the problem

Problem

Complete the solution function so that, given a string n_str consisting of only numbers, it converts n_str to an integer and returns it.

Example input and output
n_strresult
ā€œ10ā€10
ā€œ8542ā€8542

My solution to the problem

class Solution {
    public int solution(String n_str) {
        int answer = Integer.parseInt(n_str);
        return answer;
    }
}
Explanation of the solution

The Integer.parseInt() function was used to return a string as an integer.