blog.itcode.devblog.itcode.dev

[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.

[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.
RWB0104
@RWBwritten at 2021-12-18 08:21:13
Programmers

시리즈 모아보기

Programmers

47 / 78
RankLanguage 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
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08