-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ 1๋จ๊ณ - ๋ฌธ์์ด ๋ง์ ๊ณ์ฐ๊ธฐ #776
base: sumiini
Are you sure you want to change the base?
Conversation
private ํจ์์ ํ์ ๋์ง ์๊ณ ์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ด ๋ถ๋ณํ์ง ๋ณ๊ฒฝ ๊ฐ๋ฅํ์ง๋ง ํ๋จ ํด๋ณด๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ ์ํด์ฃผ์
จ์ต๋๋ค ๐
๋ค์ ๋ฏธ์
์ ์งํํ๊ธฐ ์ ๋ช๊ฐ์ง ์๊ฐ๊ฑฐ๋ฆฌ๋ฅผ ๋จ๊ฒจ๋์์ผ๋ ํ์ธ ๋ถํ๋๋ ค์ ๐
private static final String DELIMITER = "//(.)\n(.*)"; | ||
|
||
public int add(final String text) { | ||
if (text == null || text.isEmpty()) return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (text == null || text.isEmpty()) return 0; | |
if (text == null || text.isEmpty()) { | |
return 0; | |
} |
https://google.github.io/styleguide/javaguide.html#s4.1.1-braces-always-used
๊ตฌ๊ธ ์คํ์ผ ๊ฐ์ด๋๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ค๊ดํธ๋ฅผ ์๋ตํ์ง ์๊ณ ์์ฑํด๋ณด์ธ์ ๐
try { | ||
String[] numbers = parse(text); | ||
return sumOfNumbers(numbers); | ||
} catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ต์์ Exception์ catch ํ๋ค๋ฉด ์ด๋ค ๋ฌธ์ ์ ์ด ์์๊น์ ?
} | ||
|
||
private String[] parse(String text) { | ||
Matcher m = Pattern.compile(DELIMITER).matcher(text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋งค๋ฒ Pattern.compile(DELIMITER)
์ ํตํด ๊ฐ์ฒด๊ฐ ์์ฑ๋์ง ์๊ณ ์ฌ์ฌ์ฉํ ์ ์๋๋ก ๋ณ๊ฒฝํด๋ณด๋๊ฑด ์ด๋จ๊น์ ?
.sum(); | ||
} | ||
|
||
private int validate(String number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ฒ ๊ณผ์ ์ ์งํํ๋ฉฐ ์ต๋ํ ์์ ๋จ์์ ํด๋์ค๋ฅผ ๋์ถํด๋ณด๋๊ฒ์ ๊ถ์ฅ๋๋ฆฝ๋๋ค.
์์๋ฅผ ๋ณด์ฅํ ์ ์๋ ํด๋์ค๋ฅผ ์ถ๊ฐํด๋ณด์ธ์ ๐
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringCalculator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ ์ํด์ฃผ์
จ์ต๋๋ค ๐
ํ๊ฐ์ง ๊ฐ์ ์ฌํญ์ผ๋ก ํ์ฌ StringCalculator
๊ฐ ๋๋ฌด ๋ง์ ์ญํ ์ ๋ด๋นํ๊ณ ์๋ค๊ณ ์๊ฐ๋ฉ๋๋ค.
๋ฌธ์์ด ํ์ฑ์ ๋ด๋นํ๋ ํด๋์ค, ๊ณ์ฐ์ ๋ด๋นํ๋ ํด๋์ค ๋ฑ์ผ๋ก ์ญํ ์ ๋๋๋ ์ฐ์ต์ ํด๋ณด์ธ์ !
private int validate(String number) { | ||
try { | ||
int num = Integer.parseInt(number); | ||
if (num < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋งค์ง๋๋ฒ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ ์์๋ก ์ ์ํ์ฌ ๊ฐ๋
์ฑ์ ๋ํ๋ณด์ธ์ ๐
https://www.slipp.net/questions/356
์๋ ํ์ธ์.
์๊ตฌ์ฌํญ๋ณ๋ก ์ปค๋ฐํ๋ ค ํ์ผ๋, ๋ชจ๋ ํ ์คํธ ์กฐ๊ฑด์ ๋ง์กฑ์์ผ์ผ ํ๋ค๋ ์๊ฐ์ ๋ถ๋ฆฌํ์ฌ ์ปค๋ฐํ๋๊ฒ ์ด๋ ค์ ์ต๋๋ค.
add ๋ด ๋งค๊ฐ๋ณ์ text๋ ๋ณ๊ฒฝ ๋ถ๊ฐํ ๊ฐ์ด๋ฏ๋ก final ์ ์ธ์ด ๋ง๋ค๊ณ ์๊ฐํ์ต๋๋ค.
๋ด๋ถ์ private ํจ์๋ค ์ค text๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๋ ๊ฒ๋ค๋ ๋ชจ๋ final์ ์ ์ธํด์ผํ๋์ง? ์๋๋ฉด, ์ด๋ฏธ add ๋งค๊ฐ๋ณ์์์ final text๋ก ๋ค์ด์์ ์๊ด์๋์ง ๊ถ๊ธํฉ๋๋ค.