Poulad Majlessi Poulad Majlessi - 3 years ago 293
Java Question

Average, minimum, maximum

so in my program is where the user enters list of numbers one at a time, and when I would end the list numbers with the "end" statement which is set to -1, and once I do that I get my average, and maximum, and minimum, my problem is that when I do get the minimum output it would be -1 everytime, I'm having trouble to remove the -1 from the array, any ideas???

import java.util.Scanner; //import scanner to user scanner tool

public class Average { //creating public class

public static void main(String[]args) { //creating public static main

Scanner input = new Scanner(System.in); //creating scanner input to grab user input

System.out.println("Please enter a list of numbers, entering -1 to end the list: ");

double[] numbers = new double[20]; //creating 20 count array
double sum = 0;
int count = 0;
double average;
int end = -1;

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

System.out.print("Enter a number: ");
numbers[i] = input.nextDouble();

if(numbers[i]== end) {

break;

}
sum+= numbers[i];
count++;
}
// gets average from user input of numbers
average = sum/count;
System.out.println("Average is: " + average);
double max = maxim(numbers);
System.out.println("Max: " + max);
double min = minim(numbers);
System.out.println("Min: " + min);

}

//method for finding out maximum number from user input

public static double maxim(double[] array) {

double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
//method for finding out minimum number from user input

public static double minim(double[] array) {

double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}

}
return minNum;
}

}

Answer Source

You need to docouple part of code that responsible for reading user input from code that compute statistics. And for remove -1 from resulting array you need simple don't put this value to result. When user input random number first check if it is not -1, and after that put it in result. Something like that:

import java.util.Scanner;  

public class Average {  

    public static void main(String[]args) { //creating public static main
        double[] numbers = readInputNumbers();
        System.out.println("Average is: " + average(numbers));
        System.out.println("Max: " + max(numbers));
        System.out.println("Min: " + min(numbers));
    }

    public double[] readInputNumbers() {
      Scanner input = new Scanner(System.in); //creating scanner input to grab user input
      System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
      double[] numbers = new double[20];
      final int endInput = -1;
      for(int i = 0; i < numbers.length; i++) {
          System.out.print("Enter a number: ");
          double nextNumber = input.nextDouble();
          if(nextNumber == endInput) {
              break;
          } else {
              numbers[i] = nextNumber;
          }
      }
      return numbers;   
  }

    public static double max(double[] array) {
        double maxNum = array[0];
        for(int i = 1; i<array.length; i++) {
            if(array[i] > maxNum) {
                maxNum = array[i];
            }
        }
     return maxNum; 
    }

    public static double min(double[] array) {
        double minNum = array[0];
        for(int i = 1; i<array.length; i++) {
               if(array[i] < minNum && array[i]!=-1) {
                minNum = array[i];
            }

        }
     return minNum; 
    }

    public static double average(double[] numbers) {
        double sum = 0;
        for(int i = 0; i < numbers.length; i++) {
            sum = sum + numbers[i];
        }
        return sum / numbers.length; 
    }

}

In jdk since 8 version your could be simplify this task like this:

import java.util.Scanner; 
import java.util.stream.*; 

public class Average {  

    public static void main(String[]args) { //creating public static main
        double[] numbers = readInputNumbers();
        DoubleSummaryStatistics statistics = DoubleStream.of(numbers).summaryStatistics();
        System.out.println("Average is: " + statistics.getAverage()));
        System.out.println("Max: " + statistics.getMax());
        System.out.println("Min: " + statistics.getMin());
    }

    public double[] readInputNumbers() {
        Scanner input = new Scanner(System.in); //creating scanner input to grab user input
        System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
        double[] numbers = new double[20];
        final int endInput = -1;
        for(int i = 0; i < numbers.length; i++) {
            System.out.print("Enter a number: ");
            double nextNumber = input.nextDouble();
            if(nextNumber == endInput) {
                break;
            } else {
                numbers[i] = nextNumber;
            }
        }
        return numbers;   
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download