What is the reason for the error "The Operator / is Undefined for the argument" in this code?

Asked

Viewed 981 times

1

 double t1 = System.currentTimeMillis();
 double t2 = System.currentTimeMillis();
System.out.print("\n Tempo Total: "+ (t2-t1)/Double.valueOf(1000)+ " segundos");

When compiling the third line, corresponding to System.out.println, makes a mistake:

Unresolved Compilation problem: The Operator / is Undefined for the argument type(s) double, Double

  • It is running normally, see: https://ideone.com/p9a7b3

  • You can put the imports of this Java file?

  • I’m a beginner. which are the Imports I should put and where should I put them, please? in your file have these: import java.util.; import java.lang.;&#A;import java.io. *; Put but did not work.

  • I changed exactly nothing in your code, I just copied and pasted without the need for any import. Rode good.

  • That strange friends, here does not run at all.Da the same error. The Operator / is Undefined for the argument type(s) double, Double

  • Exception in thread "main" java.lang.Error: Unresolved Compilation problem: The Operator / is Undefined for the argument type(s) double, Double

  • I ordered the import because it could be that your Double was not the Java Double but another one that you imported unintentionally.

  • After reading the comment from @sullyvan.Nunes I decided to compile the original code and everything worked out too (example in Ideone). That said I’ve seen similar errors happening in Eclipse, Jaspersoft Studio, etc. I believe something in the build chain may be behaving differently than the standard Oracle compiler.

  • What is the Java version? This only works from Java 5 (released in 2004). If your Java is a more dinosaur version, it won’t even work.

  • Thanks guys, to using the eclipse is the java and the latest!.

  • 1

    Yesterday was the day of weird mistakes. This is that other on the same day? Cosmic rays must have hit us ugly yesterday.

Show 6 more comments

2 answers

4


The problem is that the compiler some tool in your build is getting lost in the time to do Unboxing of the kind Double to the primitive double, what is needed to enable division. To be more didactic, there are clear rules for Unboxing and widening you find in specialized Java SE Programmer certification books. Compiling your example directly with the Oracle compiler (see working on Ideone) everything worked as expected, which leads me to believe that some of your tools are causing the problem.

That said, try the following:

System.out.print("\n Tempo Total: "+ ((t2-t1)/ 1000.0d)+ " segundos");

That way you’re using a literal like double and no form of Unboxing is necessary.

  • Total time: 0.0 seconds Congratulations Anthony! , that’s right, thank you very much! , solved! : D

  • If I do double P1 = 1 / Double.valueOf(array.length - 1); How does it look? double double P1 = (1 /(array.length - 1)d);???

  • Thiago, give doubt never use valueOf (this method returns a Double and not a double). If you want fractional values use literals double and/or casting: For example: double p1 = 1.0d / (array.length -1); or double p1 = 1 / ((double) array.length -1);

  • Thanks again!

0

Try closing the parenthesis once more by making your operation isolated, this way:

double t1 = System.currentTimeMillis(); 
double t2 = System.currentTimeMillis();
System.out.print("\n Tempo Total: "+ ((t2-t1)/Double.valueOf(1000))+ " segundos
  • Continued with the same error My friend :(but thanks for trying, Exception in thread "main" java.lang.Error: Unresolved Compilation problem: The Operator / is Undefined for the argument type(s) double, Double

Browser other questions tagged

You are not signed in. Login or sign up in order to post.