Skip to content
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

[문자열계산기] 조영욱 미션 제출합니다.Submit #1889

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# java-calculator-precourse
# java-calculator-precourse
1.사용자입력받기
2.커스텀 구분자 적용하기
3.구분자 분리하기
4.계산하기
5.반환하기
59 changes: 58 additions & 1 deletion src/main/java/calculator/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
package calculator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import camp.nextstep.edu.missionutils.Console;
public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
System.out.println("덧셈할 문자열을 입력해 주세요.");

String input = Console.readLine(); // 사용자 입력 받기
try {
int result = add(input);
System.out.println("결과: " + result);
} catch (IllegalArgumentException e) {

}
}

public static int add(String input) {
if (input == null || input.isEmpty()) {
return 0; // 빈 문자열일 경우 0 반환
}

String delimiter = ",:"; // 기본 구분자 (쉼표, 콜론)

// 커스텀 구분자 확인
if (input.startsWith("//")) {
int delimiterIndex = input.indexOf("\\n");
//System.out.println(delimiterIndex);
if (delimiterIndex != -1) {
// 커스텀 구분자 추출
StringBuffer buf = new StringBuffer(delimiter);
buf.insert(0,input.substring(2, delimiterIndex));
delimiter = buf.toString();
//System.out.println(delimiter);
input = input.substring(delimiterIndex + 1); // 숫자 문자열로 변경
}
}

// 커스텀 구분자 및 기본 구분자로 분리
String[] tokens = input.split("[" + delimiter + ",:]");
int sum = 0;

// 숫자 합산 및 예외 처리
for (String token : tokens) {
if (!isNumeric(token)) { // 숫자가 아닌 경우 예외 발생
throw new IllegalArgumentException("잘못된 입력 값: " + token);
}
sum += Integer.parseInt(token); // 문자열을 정수로 변환하고 합산
}

return sum;
}

// 숫자 여부 확인 메서드
private static boolean isNumeric(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}