We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
hey, as there are a few pwople in java so i thought i should post my solution with explaination:
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String S = bufferedReader.readLine();
try{
//to parse the string as integer
int number= Integer.parseInt(S);
//print the integer value
System.out.println(number);
//if numberformatexception is thrown then print "Bad String"
}catch(NumberFormatException e){
System.out.println("Bad String");
//close the bufferreader
}finally{
bufferedReader.close();
}}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 16: Exceptions - String to Integer
You are viewing a single comment's thread. Return to all comments →
hey, as there are a few pwople in java so i thought i should post my solution with explaination: public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}