Scanner scan = new Scanner(; int count = 0; int sum = 0; while (true) { int num = scan.nextInt(); if (num == -1) break; count++; sum = sum + num; } double med = sum / count; System.out.println(med); This is what I wrote, but the program doesn't calculate the solution correctly. when I use the numbers 1,2,2,4,5,-1 it's solution is 2.0, not 2.8. same for 4,3,2,1,-1. I can't figure out why, since the break happens before the calculation, the -1 shouldn't be counted neither for sum, nor for count. What am I not seeing? Thanks for the help in advance.