Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# java-calculator-precourse
# java-calculator-precourse

### 1. 구분자와 양수로 구성된 문자열 입력받기

- 기본 구분자 문자열 입력
- 커스텀 구분자 문자열 입력

### 2. 각 숫자의 합 반환

- 커스텀 구분자 존재 여부 확인
- 문자와 숫자 분리
- 숫자 더하기 및 출력

### 3. IllegalArgumentException 발생 조건
48 changes: 47 additions & 1 deletion src/main/java/calculator/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
package calculator;

import java.util.Scanner;
import java.util.Vector;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Scanner sc = new Scanner(System.in);
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.

다음에는 미션에서 적혀있는대로 camp.nextstep.edu.missionutils에서 제공하는 Console을 사용해서 입력을 구현해주세요!

Vector<Integer> number = new Vector<>();
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.

백터를 쓰신 이유가 있을까요? 이유가 없다면 Vector 외에도 다른 선택지가 많을 것 같아요!
참고 글

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Vector말고 ArrayList로 불필요한 동기화를 줄여서 성능도 향상시켜보면 어떨까용?
Vector vs ArrayList참고자료
Vector을 자바에서 쓰지않는 이유

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

저도 덕분에 자바의 여러 중요한 부분들을 공부하게된 것 같아 좋네요 :)

String words;
char custom;

// 문자열 입력
System.out.println("덧셈할 문자열을 입력해 주세요.");
words = sc.next();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nextLine()대신 공백기준으로 한단어만 입력받는 next()를 사용하신 이유가 있을까용?


// 커스텀 구분자 확인
if(words.charAt(0)=='/' && words.charAt(1)=='/' && words.charAt(3)=='\\' && words.charAt(4)=='n') {
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.

여기도 정규표현식 쓰면 간결해질 것 같아요!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

words의 index 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.

맞네요 생각 못했어요

custom = words.charAt(2);

// 숫자와 문자 분리
for(int i=5; i<words.length(); i++) {
char word = words.charAt(i);
if(word >= 48 && word <= 57) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

아마 숫자인지를 확인하기위해 ASCII값을 직접 비교하는 로직을 넣으신 거겠죠..?
Java에서는 Character.isDigit()같은 유용한 API를 활용하면 가독성도 좋아지고 로직도 간단해질 것 같아용!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다!

number.add(word-48);
}
else if(word != custom) {
// 예외 처리를 위한 예비출력
System.out.println("예외");
}
}
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.

Character 단위로 하면 1의자리를 제외한 숫자는 표현이 안되지 않을까요...? ex) 입력 : 32 -> 결과 : 5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다

}
else { // 기본 구분자의 숫자와 문자 분리
for(int i = 0; i < words.length(); i++) {
char word = words.charAt(i);
if(word >= 48 && word <= 57) {
number.add(word-48);
}
else if(word != ',' && word != ':') {
// 예외 처리를 위한 예비출력
System.out.println("예외");
}
}
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.

이렇게 순회하면서 number 이라는 백터에 담을 수도 있지만 split을 통해 쪼개는 것이 더 간단할 것 같아요!
만약에 그냥 숫자인 문자만 얻기 위함이라면 Stream과 filter을 통해 보다 쉽게 결과를 얻을 수 있을 것 같아요!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다.

}

// 숫자의 합 출력
int sum = 0;
for(int i = 0; i < number.size(); 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 API와 Reduce를 써도 좋을 것 같아요! for문을 쓰더라도 for (int value : number)이었나 여튼 원소를 순회하는게 가독성이나 효율성 측면에서 더 좋을 것 같아요!

sum += number.get(i);
}
System.out.println(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.

주석 다셨던 단위로 함수를 분리하면 코드의 흐름이 한눈에 들어올 것 같아요!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다