-
Notifications
You must be signed in to change notification settings - Fork 9
[문자열 계산기] 표현록 미션 제출합니다. #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
39319aa
20c2c23
aa4680c
a12bd35
c2ca88d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,10 @@ | ||
| # java-calculator-precourse | ||
| # java-calculator-precourse | ||
|
|
||
| ## 구현할 기능 목록 | ||
| 1. 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다. | ||
| - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 | ||
| # | ||
| 2. 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다. | ||
| - 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. | ||
| # | ||
| 3. 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,46 @@ | ||
| package calculator; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| // 값 입력받기 | ||
| System.out.println("덧셈할 문자열을 입력해 주세요."); | ||
| String word = Console.readLine(); | ||
|
|
||
| // 기본 구분자 설정 | ||
| ArrayList<String> operatorList = new ArrayList<>(); | ||
| operatorList.add(","); | ||
| operatorList.add(":"); | ||
|
|
||
| // 커스텀 구분자 인식 | ||
| Pattern p = Pattern.compile("//\\D\\\\n"); | ||
| Matcher m = p.matcher(word); | ||
| if (m.find()) { | ||
| operatorList.add(String.valueOf(word.charAt(m.start()+2))); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. m.start()에 +2를 하는 이유가 뭔가요?? |
||
| word = word.substring(m.end()); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정규표현식으로 처리한 김에 좀 더 확장해서 정규표현식으로 추출하도 좋을 것 같아요 |
||
|
|
||
| // 구분자 정리 | ||
| String[] operatorArray = new String[operatorList.size()]; | ||
| for (int i = 0; i < operatorList.size(); i++) | ||
| operatorArray[i] = operatorList.get(i); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인덱스로 접근하기보다는 원소로 접근하는게 좋을 것 같아요! 1별다른 이유가 없다면 operatorArray도 배열이 아니라 컬렉션, 가령 ArrayList 등으로 표현하면 확장하기 쉬울 것 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동의합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 배열로 하다가 중간에 ArrayList로 변경했는데 ArrayList가 더 좋아보여요! |
||
| String operator = String.join("|", operatorArray); | ||
|
|
||
| // 숫자 검수 및 연산 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 처음이라 미숙해서 함수별로 분리 안하였는데 분리하는게 좋을 것 같습니다 |
||
| int sum = 0; | ||
| String[] result = word.split(operator); | ||
| for (String s : result) { | ||
| int i = Integer.parseInt(s); | ||
| if (i <= 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0도 예외로 생각하신건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 입력조건에 양수라고 되어있어 따로 0과 음수에 대한 예외처리를 하지않았는데 수정할 때 참고해볼게요..! 예외처리를 신경쓰신 것 같아요! |
||
| throw new IllegalArgumentException(); | ||
| sum += i; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분을 stream으로 표현하면 가독성이 더 높고 효율적일 것 같아요 |
||
|
|
||
| System.out.printf("결과 : %d%n", sum); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 파일도 주석을 달지 말고 그 부분마다 함수를 분리하는게 더 좋을 것 같아요 |
||
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.
이부분도
List.of로 한 번에 추가할 수 있을 것 같아요!