+ 1

Can you figure out why last test case of "Mathematics" problem in Code Coach is not getting Passed ?

Have anyone passed that test case, if yes can share the your code or figure out what is wrong with my Code ? https://code.sololearn.com/cVHs8f3Qqk8s/?ref=app

5th Jun 2022, 11:16 AM
Mr.Curious
Mr.Curious - avatar
11 Réponses
+ 1
Mr.Curious I just run code in Code Playground and found problem here: a.charAt(i++); I was getting IndexOutOfBoundException; So I just added try catch block here and worked fine. double f = 0; try { f = calc(a[i]); } catch (Exception e1) { }
5th Jun 2022, 6:11 PM
A͢J
A͢J - avatar
+ 2
Mr.Curious why did you choose Java? This task is easier in Python. If you have problems with 2 test you can add a counter. Use index++ if the answer us right. And if in the end of the code counter is 0 write none. Look on my Python code. https://code.sololearn.com/cx59Wr2G5i01/?ref=app
8th Jun 2022, 5:49 AM
aewerdev
aewerdev - avatar
+ 1
Mr.Curious Everything is right, just add try catch block because problem is in taking input int k = 0; try { double n = Double.parseDouble(sc.nextLine()); String[] a = sc.nextLine().trim().split(" "); for(int i = 0; i < a.length; i++) { double f = calc(a[i]); if (f == n) { System.out.print("index " + i); k = 1; break; } } } catch (Exception e) { } if(k <= 0) System.out.println("none"); https://code.sololearn.com/c2KlEBQ069q2/?ref=app
5th Jun 2022, 1:04 PM
A͢J
A͢J - avatar
+ 1
Mr.Curious Working for me. Just copy whole code and paste there.
5th Jun 2022, 1:42 PM
A͢J
A͢J - avatar
+ 1
A͢J , Thanks it worked but why it didn't worked if I placed the catch block after the full statement ( after printing none).
5th Jun 2022, 3:07 PM
Mr.Curious
Mr.Curious - avatar
+ 1
Mr.Curious Because after getting exception try block will immediately stop to execute other statement and it will not reach at print statement so nothing will print. For example if you have 5 lines of code in try block and print statement is on 5th line but exception came at 3rd or 4th line so 5th line will not be execute and nothing will be print.
5th Jun 2022, 3:35 PM
A͢J
A͢J - avatar
0
Please 👉 tag the relevant programming language 👉 mention course name/ task If you need help with your code, please link your code edit: Mr.Curious As noted by the "pro" label, not every user can access the pro-task. Hence, you need to describe the task to those who are not able to access it.
5th Jun 2022, 11:19 AM
Lisa
Lisa - avatar
0
Please read my previous reply: What is the task? Tag the relevant programming language. In order to help you, you need to tell us 👉what the task is.
5th Jun 2022, 11:49 AM
Lisa
Lisa - avatar
0
Lisa , Task is clearly mentioned there is problem in Code Coach named Mathematics. Edit : lisa, It is not a pro question, everyone has its access.
5th Jun 2022, 11:57 AM
Mr.Curious
Mr.Curious - avatar
0
So it means, statement should work after the try block, so why it is not working in case I am giving it immediately after input : import java.util.*; public class Program { static String a[]; public static void main(String[] args) { int k=0; double n=0; try { Scanner sc=new Scanner(System.in); n=Double.parseDouble(sc.nextLine()); a=sc.nextLine().trim().split(" "); } catch (Exception e) {} for(int i=0;i<a.length;i++) { double f=calc(a[i]); if (f==n) { System.out.print("index "+i); k=1; break; } } if(k==0) System.out.print("none"); }
5th Jun 2022, 3:50 PM
Mr.Curious
Mr.Curious - avatar
0
Daniil Pivovarov , It's easier just because it has an inbuilt library function.
8th Jun 2022, 5:58 AM
Mr.Curious
Mr.Curious - avatar