+ 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
11 Antworten
+ 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) {
}
+ 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
+ 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
+ 1
Mr.Curious
Working for me.
Just copy whole code and paste there.
+ 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).
+ 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.
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.
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.
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.
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");
}
0
Daniil Pivovarov , It's easier just because it has an inbuilt library function.