Java Day 16 - Exceptions - String to Integer

Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String.

  1. import*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Solution {
  7. public static void main(String[] args) {
  8. Scanner in = new Scanner(;
  9. String string =;
  10. in.close();
  11. try {
  12. Integer integer = Integer.parseInt(string);
  13. System.out.println(integer);
  14. } catch (NumberFormatException numberFormatException) {
  15. System.out.println("Bad String");
  16. }
  17. }
  18. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]

donate a cup of tea :)

Join Our Facebook Group

Share this solution

download android app

Popular HackerRank Java Solutions is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube