-2

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!

  • 2
    Does this answer your question? [Int division: Why is the result of 1/3 == 0?](https://stackoverflow.com/questions/4685450/int-division-why-is-the-result-of-1-3-0) – jhamon Feb 14 '20 at 16:15

3 Answers3

0

Try the following:

double average = 1.0 * sum / count;

This should explicitly convert the expression to double.

Sergei
  • 5,942
  • 1
  • 19
  • 32
  • 1
    1) This should be a comment. 2) You should explain *why*. – FoggyDay Feb 14 '20 at 16:16
  • @FoggyDay 1) not sure why this should be a comment, if it answers the question, and 2) I've provided the explanation. I doubt that adding a reference to the Java specification would improve my answer. – Sergei Feb 14 '20 at 16:17
  • The answer is fine. And according to the site guidelines you're not supposed to put code in comments. – WJS Feb 14 '20 at 17:39
0

I think your problem is that your using integer division. In your code, sum and count are integers. By instead multiplying sum by 1.0, the sum will be converted to a double. And the result of the division will be a double also.

double average = 1.0*sum/count;
0

Since your are streaming integers from a file, you could just rely on IntStream::average as well, which returns a double.

    int[] intArray = Files.lines(Paths.get("/tmp/input3"))
            .map(String::trim)
            .mapToInt(Integer::parseInt)
            .toArray();
    double average = Arrays.stream(intArray).average()
            .orElse(0d);

    for (int i = 0; i < intArray.length; i++) {
        if (intArray[i] > average) {
            System.out.print(intArray[i] + " > ");
        } else {
            System.out.print(intArray[i] + " ");
        }

        if (i % 5 == 4) {
            System.out.println();
        }
    }


    Map<Integer, Integer> occurences = Arrays.stream(intArray)
            .boxed()
            .collect(Collectors.toMap(
                    i -> i,
                    i -> 1,
                    Integer::sum
            ));

    System.out.println("\n***** Frequency Graph *****\n");
    occurences.keySet().stream()
            .sorted()
            .forEach(k -> System.out.println(k + " = " + "*".repeat(occurences.get(k))));

    System.out.println("\nAverage of the numbers in the text file is : " + average);
cghislai
  • 1,599
  • 11
  • 22