• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning values from arrays

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ciara O'Hara
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic