S School - 11 months ago 45

Java Question

`package HelpMe;`

import java.util.Scanner;

import java.text.DecimalFormat;

public class StackOverFlowHelp {

public static void main(String[] args) {

//

DecimalFormat df = new DecimalFormat("00.0");

Scanner input = new Scanner(System.in);

int[] myArray = {45, 38, 27, 46, 81, 72, 56, 61, 20, 48, 76, 91, 57, 35, 78};

int totalnuminArray = 0;

int sumofallnumbersinArray = 0;

int largestnumArray = 45; //Pick a random # from the actual array to compare with!

int indexoflargestnumArray = 0;

int smallestnumArray = 91; //Pick a random # from the actual array to compare with!

int indexofsmallestnumArray = 0;

//

//Find the total number of elements in myArray:

for(int i = 0; i<myArray.length ; i++){

totalnuminArray+=1;

}

System.out.println("Total amt. of elements in the array: " + totalnuminArray);

//Finding the sum of all numbers in the ARRAY:

for(int i = 0; i<myArray.length; i++){

sumofallnumbersinArray = sumofallnumbersinArray+myArray[i];

}

System.out.println("Sum of all of the numbers in the array: " + sumofallnumbersinArray);

//Finding the largest of all numbers in the ARRAY & the index of that:

largestnumArray = 1;

for(int i = 0; i<myArray.length; i++){

if(myArray[i] > largestnumArray){

largestnumArray = myArray[i];

indexoflargestnumArray = i;

}

}

System.out.println("The largest number in the array is: " + largestnumArray);

System.out.println("The index of the largest number in array is: " + indexoflargestnumArray);

//Finding the smallest of all numbers in the ARRAY & the index of that:

for(int i = 0; i<myArray.length; i++){

if(myArray[i] < smallestnumArray){

smallestnumArray = myArray[i];

indexofsmallestnumArray = i;

}

}

System.out.println("The smallest number in the array is: " + smallestnumArray);

System.out.println("The index of the smallest number in array is: " + indexofsmallestnumArray);

//Finding the average of the ARRAY:

double avgofArray = sumofallnumbersinArray/totalnuminArray;

System.out.println("The average of the ARRAY is: " + df.format(avgofArray));

}

}

The output is as follows:

`Total amt. of elements in the array: 15`

Sum of all of the numbers in the array: 831

The largest number in the array is: 91

The index of the largest number in array is: 11

The smallest number in the array is: 20

The index of the smallest number in array is: 8

The average of the ARRAY is: 55.0

The problem is when it outputs "The average of the ARRAY is: 55.0."

The number that it should've outputted should have been 55.4 but it's 55.0!!!

Why? I tried to typecast it as double before I outputted it.

I'm now stuck and don't know what to do. This is for beginning java. Can you please help me?

Thanks for reading!

Answer Source

```
double avgofArray = sumofallnumbersinArray/totalnuminArray;
```

Change that to

```
double avgofArray = (double)sumofallnumbersinArray/totalnuminArray;
```

That converts the first number to `double`

, which forces the division to be evaluated in floating-point.