문서 편집 권한이 없습니다. 다음 이유를 확인해주세요: 요청한 명령은 다음 권한을 가진 사용자에게 제한됩니다: 사용자. 문서의 원본을 보거나 복사할 수 있습니다. ==개요== ;<nowiki>HR30 Day 16: Exceptions - String to Integer</nowiki> * https://www.hackerrank.com/challenges/30-exceptions-string-to-integer/problem {{HR30 헤더}} {{HR30 10-19}} |} ==Java== <syntaxhighlight lang='Java'> import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); String S = in.next(); try { int num = Integer.parseInt(S); System.out.println(num); } catch (NumberFormatException e) { System.out.println("Bad String"); } } }</syntaxhighlight> ==PHP== {{참고|HR30 Day 16: Exceptions - String to Integer/PHP}} <syntaxhighlight lang='PHP'> <?php $handle = fopen ("php://stdin","r"); fscanf($handle,"%s",$S); echo is_numeric($S) ? $S : 'Bad String'; </syntaxhighlight> <syntaxhighlight lang='PHP'> <?php $handle = fopen ("php://stdin","r"); fscanf($handle,"%s",$S); try { new SplFixedArray($S); echo $S; } catch (TypeError $e) { echo 'Bad String'; } </syntaxhighlight> <syntaxhighlight lang='PHP'> <?php $handle = fopen ("php://stdin","r"); fscanf($handle,"%s",$S); function throwMyException() { throw new Exception('Bad String'); } try { ($S>0)? 0 : throwMyException(); echo $S; } catch (Exception $e) { echo $e->getMessage(); } </syntaxhighlight> <syntaxhighlight lang='PHP'> <?php $handle = fopen ("php://stdin","r"); fscanf($handle,"%s",$S); try { new ReflectionClass('ReflectionClass'.!is_numeric($S)); echo $S; } catch (Exception $e) { echo 'Bad String'; } </syntaxhighlight> <syntaxhighlight lang='PHP'> <?php $handle = fopen ("php://stdin","r"); fscanf($handle,"%s",$S); try { new ReflectionClass('ReflectionClass'. ((int)$S.'' !== $S)); echo $S; } catch (Exception $e) { echo 'Bad String'; } </syntaxhighlight> ==Python== <syntaxhighlight lang='python'> #!/bin/python3 import sys S = input().strip() try: num = int(S) print(num) except ValueError: print("Bad String") </syntaxhighlight> ==Ruby== <syntaxhighlight lang='ruby'> #!/bin/ruby S = gets.strip begin puts Integer(S) rescue puts 'Bad String' end </syntaxhighlight> 이 문서에서 사용한 틀: 틀:Ed (원본 보기) 틀:HR30 10-19 (원본 보기) 틀:HR30 헤더 (원본 보기) 틀:언어아이콘 (원본 보기) 틀:언어이미지 (원본 보기) 틀:참고 (원본 보기) HR30 Day 16: Exceptions - String to Integer 문서로 돌아갑니다.