Regardless of what I try, the program outputs the average as the integer "4" when the average should be "4.78". I have tried changing the integers to doubles, but to no avail.
The rest of the program works as intended, though. It is supposed to print how many times each value in the text files appears by putting an asterisk next to it and it is supposed to indicate when a number is larger than the average by having a ">" appear next to it in the output.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws IOException {
int i = 0, count = 0, sum = 0;
Scanner file = new Scanner(new File("input3.txt"));
int[] frequency = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int[] arr = new int[50];
int current_num;
while (file.hasNext()) {
current_num = file.nextInt();
sum += current_num;
arr[i++] = current_num;
count++;
frequency[current_num]++;
}
int average = sum / count;
for(i=0; i<arr.length; i++){
if(arr[i] > average){
System.out.print(arr[i] + " > " );
}
else{
System.out.print(arr[i] + " ");
}
if(i%5==4){
System.out.println();
}
}
System.out.println("\n***** Frequency Graph *****\n");
for(i=0; i<frequency.length; i++){
System.out.print(i + " = ");
for(int j=0; j<frequency[i]; j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("\nAverage of the numbers in the text file is : " + average);
}
}
The numbers in the "input3.txt" file are:
5
8
8
1
5
0
6
6
5
3
4
0
6
8
5
4
9
5
8
0
4
7
4
2
0
9
8
3
5
5
5
7
5
7
1
4
4
0
7
4
8
4
2
4
9
8
8
2
3
4
Any help would be appreciated!