title: Finding largest number in Java
body: <p>I am trying to find the large number in java by using the integer parameter. I converted the integer value to string but the problem is, it failed on the run and did not show the expected result.</p>
  <p>Here is the case:</p>
  <p>Given an integer n, it returns the largest number that contains exactly n digits only when n is non-zero.</p>
  <ul>
  <li>For n = 1, the output should be largestNumber(n) = &quot;9&quot;.</li>
  <li>For n = 2, the output should be largestNumber(n) = &quot;99&quot;.</li>
  <li>For n = 3, the output should be largestNumber(n) = &quot;999&quot;.</li>
  <li>For n =0, the output should be largestNumber(n) = &quot;-1&quot;.</li>
  </ul>
  <p><strong>Constraints
  0 ≤ |n| ≤ 10</strong></p>
  <p>Here is the java code :</p>
  <pre><code>class Result {
    /*
     * Complete the 'largestNumber' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts INTEGER n as parameter.
     */
  public static String largestNumber(int n) { // Write your code here
  String res = Integer.toString(n);
  return res;
  }
                     
  }public static void main(String[] args) throws IOException {
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv(&quot;OUTPUT_PATH&quot;)));
  int n = Integer.parseInt(bufferedReader.readLine().trim()); String result = Result.largestNumber(n);
          bufferedWriter.write(result);
          bufferedWriter.newLine();
          bufferedReader.close();
          bufferedWriter.close();
      }
  }
  </code></pre>
  <p>Here is the result of the screenshot.</p>
  <p><a href="https://i.stack.imgur.com/ddiCP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ddiCP.png" alt="Result" /></a></p>