• Post Reply Bookmark Topic Watch Topic
  • New Topic

error operator + is undefined  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could not get any help on my previous code, so I switched it up a bit and still cannot get it to compile. Any ideas what is happening? these arrays are very annoying.
Error: The operator + is undefined for the argument type(s) double, double[]
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operator + is undefined for the argument type(s) double, double[] 

The + operator is for adding together two numeric values.  An array is not a numeric value so the compiler gives the error message.
If the array reference was replaced by an element in the array, the + would be valid. 
Access an element in an array by using array notation with an int index value:  [intVal]
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is here:

The problem is that num2 is an array and you want to access an element in that array, but you are not doing that.
For this to work as you most likely want you would need to do something like this:
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pete Did you compile and execute the code you posted?  Try it and see what you get.

Or look at what the OP got when he copied and pasted the code.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have fixed the code accordingly, it compiles I get an answer like
The answer to1.0[D@55f96302 Equals 2.0

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have fixed the code accordingly, it compiles I get an answer like
The answer to1.0[D@55f96302 Equals 2.0 

Yes. Is that OK, or What output would you like to see?
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did compile and run the program @Norm. I got what the OP gets now. I was first concerned about getting the program to run.

I thought that logic problems can be solved after it gets past the compile errors considering this is a smaller program.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was confused about the incorrect code looking like it could be copied and pasted into the OPs code.
What about this to show how to add a double to an element of an array:
 
Pete Letkeman
Ranch Foreman
Posts: 904
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Norm, I do realize that perhaps I should not have given the answer to the compile time problem so quickly with this being the Beginning Java forum.

@Kennith, it looks like your output is not displaying the element of the array, but the array object itself.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:. . . I should not have given the answer . . . so quickly with this being the Beginning Java forum.
I don't think you have done anything wrong.
@Kennith, it looks like your output is not displaying the element of the array, but the array object itself.
Common problem: because arrays don't have overridden toString() methods, so we get lots of questions about that sort of output.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!