Need help converting from int to double

• March 26th, 2013, 02:58 PM
newmanj8
Need help converting from int to double
I know you have to cast in double but I don't know how to put it I tried everything... so i think, the program sorts numbers from an arraylist and then checks to see if your right in the int[] sorted values

Code Java:

```  public void testSortDecimal() {   int[] unsorted = {1.1, .9, 3.2}; int[] sorted = { .9, 1.1, 3.2}; IterativeSorter is = new IterativeSorter(unsorted); int[] a = is.sort(); assertArrayEquals(sorted, a); }```
• March 26th, 2013, 03:07 PM
Norm
Re: Need help converting from int to double
Why are you trying to initialize an int array with double values?
Why not define the array as double?

Do you know how to use casting? Put the cast to type in ()s
For example double=2.3 to int=2 by using (int):
Code :

` System.out.println("cast to int: "+(int)2.3); // cast to int: 2`
• March 26th, 2013, 03:15 PM
newmanj8
Re: Need help converting from int to double
I would but Im not allowed to change the class for the project so i can't change the array, is there a way I can cast double into the method I tried it this way but got errors..

Code Java:

```  int[] unsorted = {(double)1.1, (double).9,(double) 3.2};```
• March 26th, 2013, 03:28 PM
Norm
Re: Need help converting from int to double
The definition of unsorted is an int array. To the right of the = should be a list of int values. The casting being done is redundant. 1.1 is a double.
Changing the (double) to (int) should work because it will change what is on the righthand side of the = to a list of int values suitable for initializing an int array.
• March 26th, 2013, 03:32 PM
newmanj8
Re: Need help converting from int to double
If I have the int array and want to have decimals be seen and come back as a decimal how can I write a method that will notice decimals?
• March 26th, 2013, 03:40 PM
Norm
Re: Need help converting from int to double
Quote:

If I have the int array and want to have decimals
You need to decide which you want to have: int or decimals

Quote:

how can I write a method that will notice decimals?
Please explain what "notice decimals" means.
• March 26th, 2013, 03:49 PM
newmanj8
Re: Need help converting from int to double
For example, this code takes the numbers that are given, -12,-5,-20,-67 then I put them in order from -67 to -5, then when I test this the program goes through and checks to make sure the sorted values match the sorted values that the program comes up with. If both the computer and my answer match up then i get a green check.

Code Java:

```@Test public void testSortAllNegative() { int[] unsorted = {-12, -5, -20, -67}; int[] sorted = {-67, -20, -12, -5}; IterativeSorter is = new IterativeSorter(unsorted); int[] a = is.sort(); assertArrayEquals(sorted, a); }```

I need to make a new method that follows this one but I want it to work with decimal numbers, I know that in order to do that I need to change the data type but the arraylist is type int but I can't edit the arraylist class. If
• March 26th, 2013, 04:24 PM
Norm
Re: Need help converting from int to double
Quote:

make a new method that follows this one but I want it to work with decimal
Can you post the code for the method you are trying to make and explain the problems you are having doing it?

Quote:

the arraylist is type int but I can't edit the arraylist class.
Where is the arraylist defined? The ArrayList class can be defined to hold Double objects: ArrayList<Double>
or arrays of doubles: ArrayList<double[]>