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.
Day 16: Exceptions - String to Integer
Day 16: Exceptions - String to Integer
Sort by
recency
|
1000 Discussions
|
Please Login in order to post a comment
for code java public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String S = sc.nextLine(); try{ int num = Integer.parseInt(S);
System.out.println(num); }catch(Exception e){ System.out.println("Bad String"); }
}
public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String S = sc.nextLine(); try{ int num = Integer.parseInt(S);
System.out.println(num); }catch(Exception e){ System.out.println("Bad String"); }
}
for js
function main() { const S = readLine();console.log(parseInt(S)?S:'Bad String') }
If anyone is trying to solve this problem in Python3. Then use this code not the pre-defined code:
For JS this worked:
But for Python I get: Error reading result file.You should use exception handling concepts.
I tried this:
and this:
None of these worked.
I think it specifically wants you to notice, in the exception, that it's a ValueError. So try specifying the exception