[Programmers / JAVA] Level 1 Convert a String to an Integer (12925)
[Programmers / JAVA] Level 1 Convert a String to an Integer (12925)
Complete the function solution, which returns the result of converting the string s into a number.
@RWBwritten at 2021-12-18 08:21:13
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
🔗 Convert a String to an Integer
Complete the function solution, which returns the result of converting the string s into a number.
- The length of s is between 1 and 5, inclusive.
- s may start with a sign (+, -).
- s consists only of a sign and digits.
- s does not start with "0".
For example, if str is "1234", it should return 1234, and if it is "-1234", it should return -1234.
str consists only of a sign (+, -) and digits, and no invalid values will be given as input.
Implement an algorithm that converts a string of digits into a number.
Using the Integer.parseInt() method, you can convert a string into a number. The sign is converted along with it, so you don't need to handle the sign separately.
JAVA
/** * Convert a String to an Integer class * * @author RWB * @since 2021.12.13 Mon 16:10:18 */ class Solution { /** * Method that returns the answer * * @param s: [String] string * * @return [int] answer */ public int solution(String s) { return Integer.parseInt(s); } }
# Programmers# Algorithm# JAVA# Level 1
