my dog learned polymorphism*
The moose likes Beginning Java and the fly likes return an int from a double array method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return an int from a double array method" Watch "return an int from a double array method" New topic
Author

return an int from a double array method

lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
I need assistance. I am trying to return the largest element location from a user input array. yes this is homework. I have to use the header for the method public static int[] locateLargest(double[][] a)I have to return an int[] which I'm just not seeing my solution on this. My thought was to do an index or a second array that holds the row/column of the larger element as it goes through the loop. I am unable to get my thoughts to work. I keep getting compiling errors that it needs to be an int[] or that the precision will be off. feedback much appreciated.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

lish McDonald wrote:I keep getting compiling errors that it needs to be an int[] or that the precision will be off. feedback much appreciated.


Instead of expecting your fellow ranchers to download your your code, and compiling it, perhaps you will get better responses if you ... describe what is the error that you are getting from the compiler, and just what is it that you don't understand about the message.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
Henry Wong wrote:
lish McDonald wrote:I keep getting compiling errors that it needs to be an int[] or that the precision will be off. feedback much appreciated.


Instead of expecting your fellow ranchers to download your your code, and compiling it, perhaps you will get better responses if you ... describe what is the error that you are getting from the compiler, and just what is it that you don't understand about the message.

Henry


Ok. At the end of my code where it says return int[]; I have replaced it with and when I compile, it tells me:
LocateLargestElement.java:29: error: possible loss of precision
int large = a[0][0];
^
required: int
found: double
LocateLargestElement.java:33: error: possible loss of precision
large = a[i][j];
^
required: int
found: double
LocateLargestElement.java:38: error: incompatible types
return large;
^
required: int[]
found: int
3 errors

Tool completed with exit code 1


if I change it to double large then it won't work because it needs to be an int[] return value.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

lish McDonald wrote:I need assistance. I am trying to return the largest element location from a user input array. yes this is homework. I have to use the header for the method public static int[] locateLargest(double[][] a)I have to return an int[] which I'm just not seeing my solution on this.


What a method returns and what it takes as arguments have nothing whatsoever to do with each other. For example, "What is the name of the tallest student in class" would take something like an array of Students as input and return a String as its output.

As for public static int[] locateLargest(double[][] a) what is the meaning of the return value? What does that array of ints represent? Note that this question has nothing to do with Java, and is only about understanding the problem in the general sense. If you do not know or cannot express precisely what that int[] represents, then writing the relevant Java code will be out of the question. (Writing the code is the easy part of programming. The hard part is understanding the problem in the abstract and expressing the solution precisely in the abstract.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

lish McDonald wrote:
... when I compile, it tells me:
LocateLargestElement.java:29: error: possible loss of precision
int large = a[0][0];
^
required: int
found: double


if I change it to double large then it won't work because it needs to be an int[] return value.


Do you know the difference between an int and a double data type? Do you know how to convert between those two data types? Or more specifically, how to cast between those two data types?

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return an int from a double array method
 
Similar Threads
Search Element in array Problem
Where to declare a method within a program?
Base Converter
2d arrays help
How to enter whole row of elements into 2d array