This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Returning values from arrays  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).

I need to return the values of the array, but I don't know how, and I can't seem to google find the answer.

Here is my class:



I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A), so I'm confused.

Any help appreciated.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ciara O'Hara wrote:I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A), so I'm confused.


The compiler isn't clever enough to know that you will always enter the for loop, therefore it is complaining that, if the for loop is not executed, there is no return statement.

Having said that, why have you got a for loop anyway ? It returns every time during the first iteration, so your code is basically the equivalent of
 
Ciara O'Hara
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne Neal,

I can ignore the for loop. I'm going to simplify it a bit more:



Now I'm getting: "Type mismatch: cannot convert from double to double[][]". I have also tried just 'double', no luck.

I want to return a value from the matrix. Thanks again.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ciara O'Hara wrote:I want to return a value from the matrix. Thanks again.


In that case you need to change the return type of your method (it is currently declared to return an array of double arrays) and also the type of the A variable.

And when you've fixed that you need to know that array indexes start from 0 and go to n - 1, where n is the length of the array.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ciara O'Hara wrote:I want to return a value from the matrix.


OK, but that's NOT what you're doing - at least in the last code you posted - because you've declared the methods as:
double[][] eg() { ...

So: What does that return?

PS: I think you could make things a lot easier for yourself (and us) if you gave these methods descriptive names. What are we supposed to think a method called 'eg()' does?

Winston
 
Marshal
Posts: 60127
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ciara O'Hara
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
OK, but that's NOT what you're doing - at least in the last code you posted - because you've declared the methods as:
double[][] eg() { ...



I know that's not what I am doing, that's what I can't figure out how to do! I was just posting a simplified example to try to illustrate my issue (hence the 'e.g.').

Back to the trial and error ;-).

Thanks

Ciara
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!