백준 1152 단어의 개수
노트북이 망가져서 정리해뒀던 자료들이 다 망가진 상황입니다... 노트북을 고치고나서 복구할 수 있는 만큼은 복구해 본다음에 올리도록 하겠습니다...
//[BOJ]단어의개수_1152
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] strArr = str.trim().split(" ");
int answer = strArr.length;
if(str.trim().isEmpty()) {
answer = 0;
}
System.out.println(answer);
}//end of main
}
원래는 substring 으로 해서 하려고했는데 끝날 ()안에 두번째 파라미터 앞까지를 잡을 때 str의 길이를 잡아서 하려고했더니 인덱스 오류가 계속 발생해서 방향을 바꿨다.
trim 글자들 사이 공백은 제외하고 맨앞글자 왼편과 맨뒷글짜 오른쪽에 나오는 공백을 지워주니 그걸로 지운 뒤 출력하도록 하였고. 공백이 하나만 있을경우 단어가 없으니 0이 나와야하는데 spilt을 쓸 때는 그게 불가능했다. 왜 그런지 이유를 찾아봤다.
// If no match was found, return this
if (off == 0)
return new String[]{this};
split의 함수를 뜯어보면 위와 같은 부분이 있다. 0을 내놓는 것이 아니라 ""값을 가진 String 을 내놓는 것이었다 그래서 당연히 문제에서는 내가 원하던 값이 나오지 않았다. 앞으로는 단순히 배우는 함수의 기능을 외우는 것이 아니라 혼자 공부하면서 해당 메소드의 코드를 뜯어볼 필요가 있다고 느꼈다. 그냥 배운대로 이건 이런 기능이야 라고 하니 기능이 가진 디테일을 놓치는 것 같다.
[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you, friend!


I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
please click it!
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)
The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Upvoted! Thank you for supporting witness @jswit.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit