알고리즘/문자열, 정렬
[알고리즘] 숫자만 추출 with Java, isDigit()
VIPeveloper
2022. 2. 27. 11:14
728x90
반응형
오늘은 주말이니까 string파트는 다 끝낸다는 마인드로 강의를 수강해야겠다.
오늘 배운 것
1. Character.isDigit(c)
숫자인지 판별해주는 메서드
System.out.println(Character.isDigit('8')); // true
2. ASCII 숫자
'0' ~ '9' : 48 ~ 57
3. 문제 풀이
import java.util.Scanner;
public class Main {
public static int solution(String str) {
int answer = 0;
char[] chars = str.toCharArray();
for (int i = 0; i < str.length(); i++) {
if (chars[i]>=48 && chars[i] <= 57){
int a = answer * 10;
int b = chars[i] - 48;
answer = (a+b);
}
}
return answer;
}
public static int solution2(String str){
String answer = "";
for (char c :
str.toCharArray()) {
if (Character.isDigit(c)) {
answer += c;
}
}
return Integer.parseInt(answer);
}
public static void main(String[] args) {
System.out.println(Character.isDigit('8'));
Scanner kb = new Scanner(System.in);
String str = kb.nextLine();
//System.out.println(solution(str));
System.out.println(solution2(str));
}
}
728x90
반응형