HackerRank #7 | Int to String / String to Int | 🇧🇷
Tags: java, int, string, hackerrank
Published: 2021-08-24 19:54:55

## Int to String

Para transformar um int em um string existem três formas:

<code> String s = String.valueOf(n); </code>

<code> String s = Integer.toString(n); </code>

<code> String s = "" + n; </code>

## String to Int

Para transformar uma string em um int:

<code> int n = Integer.parseInt(s); </code>

================

## Java Int to String

[Neste exercício](https://www.hackerrank.com/challenges/java-int-to-string/problem), caso conseguisse transformar um int em um String, o console devolve "Good job". Em caso contrário, devolve "Wrong answer".

<code>
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);
 }
}
</code>

============

Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:

- [HackerRank #6 | Scanner e End-of-file](https://dev.to/beatrizmaciel/hackerrank-6-scanner-e-endoffile-462c)
- HackerRank #7 | Int to String / String to Int
- [HackerRank #8 | Date and Time](https://dev.to/beatrizmaciel/hackerrank-8-date-and-time-1f78)
- [HackerRank #9 | Static Initializer Block](https://dev.to/beatrizmaciel/hackerrank-9-static-initializer-block-48ha)
- [HackerRank #10 | Currency Formatter](https://dev.to/beatrizmaciel/hackerrank-10-currency-formatter-22oj)
- [HackerRank #11 | DataTypes](https://dev.to/beatrizmaciel/hackerrank-11-datatypes-2e9h)
- [HackerRank #12 | Strings Introduction](https://dev.to/beatrizmaciel/hackerrank-12-strings-introduction-2d1n)
- [HackerRank #13 | Substring Comparisons](https://dev.to/beatrizmaciel/hackerrank-13-substring-comparisons-57ha)
- [HackerRank #14 | Abstract Class](https://dev.to/beatrizmaciel/hackerrank-14-abstract-class-3h5d)
- [HackerRank #18 | BigInteger](https://dev.to/beatrizmaciel/hackerrank-18-biginteger-4jad)
- [HackerRank #19 | Loops II](https://dev.to/beatrizmaciel/hackerrank-19-loops-ii-385n)
- [HackerRank #20 | String Reverse](https://dev.to/beatrizmaciel/hackerrank-20-string-reverse-nec)
- [HackerRank #23 | Instanceof keyword](https://dev.to/beatrizmaciel/hackerrank-23-instanceof-keyword-3lh6)
- [HackerRank #26 | Generics](https://dev.to/beatrizmaciel/hackerrank-26-generics-55p8)
- [HackerRank #27 | 1D Array](https://dev.to/beatrizmaciel/hackerrank-27-1d-array-5f80)
- [HackerRank #28 | Anagrams](https://dev.to/beatrizmaciel/hackerrank-28-anagrams-boi)
- [HackerRank #33 | Arraylist](https://dev.to/beatrizmaciel/hackerrank-33-arraylist-2lj1)
- [HackerRank #34 | Exception Handling Try / Catch](https://dev.to/beatrizmaciel/hackerrank-34-exception-handling-try-catch-144f)
- [HackerRank #36 | Exception Handling](https://dev.to/beatrizmaciel/hackerrank-35-exception-handling-44e)
- [HackerRank #37 | List](https://dev.to/beatrizmaciel/hackerrank-37-list-4gmj)
- [HackerRank #38 | SubArray](https://dev.to/beatrizmaciel/hackerrank-38-subarray-1693)
- [HackerRank #39 | HashSet](https://dev.to/beatrizmaciel/hackerrank-39-hashset-3jb3)
- [HackerRank #40 | Java Dequeue](https://dev.to/beatrizmaciel/hackerrank-40-java-dequeue--497h)