Question:
Write a method named truncate() that takes an array of doubles and returns an array of ints. HELPPP!!!!?
anonymous
2010-11-11 12:04:02 UTC
Write a method named truncate() that takes an array of doubles, and returns an array of ints representing the "floored" values from all the doubles. Note that Math.floor() returns a double, which must then be cast as an int.

This is what i have... but when i test it in my tester class, i get this result --> [I@cf8583
which is a whole bunch of nothing. Not sure what's going on... any help or suggestions would be awesome. THANK YOU.




public static int[] truncate(double[] regularValues)
{
int[] newValues = new int[regularValues.length];
int count = 0;

while (count < regularValues.length)
{
double holder = Math.floor(regularValues[count]);
newValues[count] = (int) holder;
count = count + 1;
}

return newValues;
}
Three answers:
The Phlebob
2010-11-11 12:16:36 UTC
I don't see anything that could be causing that, but I have to wonder how you seem to get a string result from a function that returns an array of ints.



Also, in C and C++ at least, just storing a double into an int will truncate it. No floor() function needed.



And as I remember, floor() will return the next lower negative number if called with a negative argument. For example:



-4.0 is the result from a floor( -3.3 )



Hope that helps.
deonejuan
2010-11-11 13:19:27 UTC
Your method looks OK. It's in the code that shows [I@cf8583



[I@cf8583 tells me we have the Object's memory address. In this case the address of newValues and not newValues[ indexed ] intValue



add this import at the top of Tester

import java.util.Arrays;



then in the main()

System.out.println( Arrays.toString( newValues ) );

// this will give the garbled

System.out.println( newValue );

// this will not

System.out.println( newValue[ 0 ] );
clamshell
2010-11-11 12:15:53 UTC
It looks like it should work.



I think we need to see your tester class to see what it's doing with the result to give you that value.



I'd also look into that Math.floor, as it may not be doing what you think it's doing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...