Skip to content

[문자열 계산기] 표현록 미션 제출합니다.#1

Open
Regyung wants to merge 5 commits into
Java-JavaScript-Language-Stuty:mainfrom
Regyung:develop
Open

[문자열 계산기] 표현록 미션 제출합니다.#1
Regyung wants to merge 5 commits into
Java-JavaScript-Language-Stuty:mainfrom
Regyung:develop

Conversation

@Regyung

@Regyung Regyung commented Feb 5, 2025

Copy link
Copy Markdown

신경써서 구현한 부분

  • 최대한 if문의 연쇄 작성을 하지 않으려고 노력을 많이 했습니다.

피드백이 필요한 부분

혼자 코딩할 당시에는 몰랐는데 첫 리뷰를 받고 나니 많이 부족하다는 걸 느꼈습니다.

  • 하나에 몰아넣은 올인원 코드 파일
  • 예외 처리가 필요한 사례들

고려한 예외

너무 생각을 얕게 했던 것 같습니다...

  • 음수 입력

앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 있다.커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다.
사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다.

@moongua404 moongua404 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1주차 미션 수고하셨습니다ㅎㅎ


System.out.printf("결과 : %d%n", sum);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인덱스로 접근하기보다는 원소로 접근하는게 좋을 것 같아요! 1별다른 이유가 없다면 operatorArray도 배열이 아니라 컬렉션, 가령 ArrayList 등으로 표현하면 확장하기 쉬울 것 같아요

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동의합니다!

if (i <= 0)
throw new IllegalArgumentException();
sum += i;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분을 stream으로 표현하면 가독성이 더 높고 효율적일 것 같아요

if (m.find()) {
operatorList.add(String.valueOf(word.charAt(m.start()+2)));
word = word.substring(m.end());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정규표현식으로 처리한 김에 좀 더 확장해서 정규표현식으로 추출하도 좋을 것 같아요

// 기본 구분자 설정
ArrayList<String> operatorList = new ArrayList<>();
operatorList.add(",");
operatorList.add(":");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분도 List.of로 한 번에 추가할 수 있을 것 같아요!

operatorArray[i] = operatorList.get(i);
String operator = String.join("|", operatorArray);

// 숫자 검수 및 연산

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 처음이라 미숙해서 함수별로 분리 안하였는데 분리하는게 좋을 것 같습니다

Pattern p = Pattern.compile("//\\D\\\\n");
Matcher m = p.matcher(word);
if (m.find()) {
operatorList.add(String.valueOf(word.charAt(m.start()+2)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m.start()에 +2를 하는 이유가 뭔가요??

// 구분자 정리
String[] operatorArray = new String[operatorList.size()];
for (int i = 0; i < operatorList.size(); i++)
operatorArray[i] = operatorList.get(i);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 배열로 하다가 중간에 ArrayList로 변경했는데 ArrayList가 더 좋아보여요!

String[] result = word.split(operator);
for (String s : result) {
int i = Integer.parseInt(s);
if (i <= 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0도 예외로 생각하신건가요?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 입력조건에 양수라고 되어있어 따로 0과 음수에 대한 예외처리를 하지않았는데 수정할 때 참고해볼게요..! 예외처리를 신경쓰신 것 같아요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants