본문 바로가기
코딩 공부방 👩‍💻👨‍💻/알고리즘

[Java] 해커랭크(HackerRank) : Java Int to String

by ByJinnie 2023. 2. 8.
반응형

[문제] 

데이터 타입 변환을 연습하는 문제입니다. -100 ~ 100 범위의 정수가 입력되면, 이를 문자로 변환해야 합니다.

문자로 변환이 될 시 "Good job"이 출력되며, 아닐 시 "Wrong answer"이 출력됩니다.

 

 

예시)

입력값 :

100

 

출력값:

Good job

 

[코드]

import java.util.*;
import java.security.*;
public class Solution {
 public static void main(String[] args) {

  DoNotTerminate.forbidExit();

  try {
   Scanner in = new Scanner(System.in);
   int n = in .nextInt();
   in.close();
   //String s=???; Complete this line below

   String s = Integer.toString(n);

   
   if (n == Integer.parseInt(s)) {
    System.out.println("Good job");
   } else {
    System.out.println("Wrong answer.");
   }
  } catch (DoNotTerminate.ExitTrappedException e) {
   System.out.println("Unsuccessful Termination!!");
  }
 }
}

//The following class will prevent you from terminating the code using exit(0)!
class DoNotTerminate {

 public static class ExitTrappedException extends SecurityException {

  private static final long serialVersionUID = 1;
 }

 public static void forbidExit() {
  final SecurityManager securityManager = new SecurityManager() {
   @Override
   public void checkPermission(Permission permission) {
    if (permission.getName().contains("exitVM")) {
     throw new ExitTrappedException();
    }
   }
  };
  System.setSecurityManager(securityManager);
 }
}

 

* 문제 출처 (Prepare > Java > Introduction > Java Int to String)

: https://www.hackerrank.com/

 

HackerRank

HackerRank is the market-leading technical assessment and remote interview solution for hiring developers. Learn how to hire technical talent from anywhere!

www.hackerrank.com

 

반응형

댓글