Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 62144
193
  • 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
 
Are we home yet? Wait, did we forget the tiny ad?
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!