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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# java-calculator-precourse
# java-calculator-precourse


1. 구분자 분리하기
2. 커스텀 구분자 지정
3. 구분자를 기준으로 분리한 각 숫자의 합 반환
4. 예외처리
71 changes: 69 additions & 2 deletions src/main/java/calculator/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
package calculator;

import camp.nextstep.edu.missionutils.Console;

import java.util.Arrays;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
//입력받기
System.out.print("input string");
String input = Console.readLine();

// 4. 예외처리(입력값 검증)
if (input == null || input.trim().isEmpty()) {
throw new IllegalArgumentException("empty");
}

//기본 구분자
String[] delimiters = {",", ":"};

//2.커스텀 구분자 확인 (\n은 커스텀 구분자로 생성할 수 없음)
// -> 커스텀 구분자를 2개이상 생성하는 경우의 코드는 아직 안짬
// -> 얘는 밑에 코드를 while문안에 넣어놓고 조건을 문장의 첫 부분이 \가 아닐때까지로 하면되지않을까..?
//그냥 커스텀 구분자는 입력 하나에 하나만 있다고 해야지...ㅎㅅㅎ
String newdelimiter="";
int newdelimitersize=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.

newdelimitersize는 없어도 될 것 같아용

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

변수명 지을때 newDelimiterSize 처럼 원래 띄어쓰기 해야되는 부분을 대문자로 표기하는 카멜케이스를 적용하면 더 좋을 것 같습니다

if(input.startsWith("//")){
int customendIndex=input.indexOf("\\n");

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(customendIndex==-1){
throw new IllegalArgumentException("Error");
}
newdelimiter = input.substring(2, customendIndex); // "//" 이후와 "\n" 사이 추출
String[] newDelimiters = new String[delimiters.length + 1]; //배열 복사를 위한 새로운 배열 생성

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

newdelimiter와 newDelimiters 가 이름이 비슷해서 헷갈릴 것 같아요

System.arraycopy(delimiters, 0, newDelimiters, 0, delimiters.length);
newDelimiters[newDelimiters.length - 1] = newdelimiter;
delimiters = newDelimiters; // 새 구분자로 업데이트
input = input.substring(customendIndex + 2); // "\n" 이후 문자열만 남김
}
Comment thread
piamin04 marked this conversation as resolved.


//split함수에 들어갈 문자열 생성
String delimiterRegex="[";
delimiterRegex+=delimiters[0];
for(int i=1;i<delimiters.length;i++){
delimiterRegex+="|";
delimiterRegex+=delimiters[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.

join인가 그거 쓰면 간단히 해결될 수 있을 것 같아요

delimiterRegex+="]";

// 구분자를 기준으로 문자열 나누기
String[] numbers = input.split(delimiterRegex);

// 빈 문자열 제거 if(delimiters.length>2&&delimiters[2].startsWith(":|,"))
if(delimiters.length>2){
numbers = Arrays.stream(numbers)
.filter(number -> !number.trim().isEmpty())
.toArray(String[]::new);
}

//String [] numbers = input.split("[,|:]");
int numberslenth =numbers.length;
//System.out.println(numberslenth);

//3. 숫자 더하기
//문자열 숫자로 변환
int sum=0;
for(int j=0;j<numberslenth;j++){
int real =Integer.parseInt(numbers[j]);
sum+=real;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

인덱스 접근으로 할 필요가 있을까요? 원소를 순회하는 방식으로 하는게 더 좋을 것 같아요!


System.out.println("sum = "+sum);
}
}
}

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.

동의합니다.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

동의합니다