๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

9. ์ˆซ์ž๋งŒ ์ถ”์ถœ - Character.isDigit(), ASCII Code ์‚ฌ์šฉํ•˜๊ธฐ ๋ณธ๋ฌธ

๐ŸŽ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์•Œ๊ณ ๋ฆฌ์ฆ˜

9. ์ˆซ์ž๋งŒ ์ถ”์ถœ - Character.isDigit(), ASCII Code ์‚ฌ์šฉํ•˜๊ธฐ

jhj.sharon 2023. 5. 8. 14:52
๋ฐ˜์‘ํ˜•

1. ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ

  • ๋ฌธ์ž์™€ ์ˆซ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ๊ทธ ์ค‘ ์ˆซ์ž๋งŒ ์ถ”์ถœํ•˜์—ฌ ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ์ž์—ฐ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ “tge0a1h205er”์—์„œ ์ˆซ์ž๋งŒ ์ถ”์ถœํ•˜๋ฉด 0, 1, 2, 0, 5์ด๊ณ  ์ด๊ฒƒ์„ ์ž์—ฐ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ฉด 1205์ด ๋ฉ๋‹ˆ๋‹ค.

 

2. ๋‚ด ํ’€์ด - Character.isDigit()

  • ์•ž์„œ ์•ŒํŒŒ๋ฒณ์„ ์ฐพ์•„๋‚ด๋Š” ๋ฉ”์„œ๋“œ Character.isAlphabet()์„ ์‚ฌ์šฉํ•œ ์ ์ด ์žˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€ ํŒ๋…ํ•˜๋Š” ๋ฉ”์„œ๋“œ Character.isDigit()๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์ด ๋•Œ str ๋ฌธ์ž์—ด์„ ๋Œ๋ฉด์„œ charAt()์œผ๋กœ ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€ ํŒ๋…ํ•˜๊ฒŒ๋œ๋‹ค.
  • ๋ฌธ์ œ์—์„œ "์ž์—ฐ์ˆ˜"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ 0์œผ๋กœ ์‹œ์ž‘ํ•  ๊ฒฝ์šฐ 0์„ ์—†์• ์•ผํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ณ  intํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๊ฐ€์žฅ ์•ž์— ๋ถ™๋Š” 0์„ ์—†์• ์ค€๋‹ค
import java.util.Scanner;

public class ExtractNumbers  {
	public int solution(String str) {
		String answer = "";		
		//๋ฌธ์ž์—ด ๋Œ๋ฉด์„œ ์ˆซ์ž ์ถ”์ถœํ•˜๊ธฐ Character.isDigit()
		for(int i =0; i<str.length(); i++) {
			char c = str.charAt(i);
			if(Character.isDigit(c)) answer+=c;
		}
		
		return Integer.parseInt(answer);
	
	}

	public static void main(String[] args) {

		ExtractNumbers  T = new ExtractNumbers();
		
		Scanner kb = new Scanner(System.in);
		String str = kb.nextLine();
		
			System.out.println(T.solution(str));
		
	}

}

 

3. ๋‹ค๋ฅธ ํ’€์ด - ASCII Code(48~57)

  • ์ˆซ์ž์˜ ASCII ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ˆซ์ž๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ˆซ์ž  '0' ~ '9'์˜ ASCII๋Š” 48~57์ด๋‹ค.
  • ์กฐ๊ฑด : x >=48 && x<=57
  • answer = answer*10 + (x-48) : ๋ฌธ์ž(c)๋ฅผ ์—ฐ์‚ฐ์ž์™€ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ASCII ์ฝ”๋“œ๊ฐ’์œผ๋กœ ์ธ์‹๋œ๋‹ค.
  • ์ฆ‰, ๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ์ถ”์ถœํ•ด answer ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์ด ๊ณผ์ •์—์„œ ASCII ์ฝ”๋“œ์—์„œ 0์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’(48)์„ ๋นผ์ฃผ๋ฉด ์ˆซ์ž์˜ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด "123"์ด ์ž…๋ ฅ๋œ๋‹ค๋ฉด
  • ์ฒซ ๋ฒˆ์งธ for loop์—์„œ x๋Š” ๋ฌธ์ž '1'์ด ์ด๋‹ค. ์ด๋•Œ answer๋Š” ์ดˆ๊ธฐ๊ฐ’์ธ 0์ด๋ฏ€๋กœ, answer = answer * 10 + (x - 48)์€ 0 * 10 + (49 - 48)์ด ๋˜์–ด answer๋Š” 1์ด ๋œ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ for loop์—์„œ x๋Š” ๋ฌธ์ž '2'์ด๋ฏ€๋กœ, answer = answer * 10 + (x - 48)์€ 1 * 10 + (50 - 48)์ด ๋˜์–ด answer๋Š” 12๊ฐ€ ๋œ๋‹ค.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ x๋Š” ๋ฌธ์ž '3'์ด๋ฏ€๋กœ, answer = answer * 10 + (x - 48)์€ 12 * 10 + (51 - 48)์ด ๋˜์–ด answer๋Š” 123์ด ๋œ๋‹ค.
  • ์ฆ‰ answer๋ฅผ ๋ฎ์–ด์“ฐ๋ฉด์„œ ์ตœ์ข…๊ฐ’์„ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค.

 

import java.util.Scanner;

public class Main  {
	public int solution(String str) {
		
        int answer = 0;
        
        for(char x : str.toCharArray()){
        	if(x>=48 && x<=57) answer = answer*10 + (x-48);
            }
            return answer;
	}

	public static void main(String[] args) {

		Main  T = new Main();
		
		Scanner kb = new Scanner(System.in);
		String str = kb.nextLine();
		
			System.out.println(T.solution(str));
		
	}

}
๋ฐ˜์‘ํ˜•
Comments