-3

I made a java command line program for calculating sum and average of an arbitary series of numbers, but I am wondering is there any better way for making it shorter and maybe without an array?

public class Main {

    public static void main(String[] args) {
        System.out.println("------------------");
        System.out.println("Program for calculating sum and average of an arbitary series of numbers");
        System.out.println("------------------");

        Scanner input = new Scanner(System.in);

        System.out.print("How many numbers do you want to calculate? ");
        int nums = input.nextInt();
        int array[] = new int[nums];
        int sum = 0;

        for (int i=0; i<nums; i++){
            System.out.print("Enter " + (i+1) + ". number: ");
            array[i] = input.nextInt();
        }

        for (int i=0; i<array.length; i++){
            sum= sum + array[i];
        }
        System.out.println("Sum is: " + sum);
        System.out.print("Average is: " + (sum/nums));
    }
}
soufrk
  • 825
  • 1
  • 10
  • 24
BaDnja
  • 15
  • 5

3 Answers3

2

You have two for loops in your code. The first one writes values to an array, and the second one reads them to get their sum. You don't really need both of those loops (or the array). You can sum the values as you read them in, then calculate the average based on the sum.

Note, though, that if you want to extend your code later to calculate other stats on the entered data (e.g., standard deviation) it will be easier to work with the data stored in an array.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1
  1. You don't need to store these numbers to an array.
  2. Usage of BufferedReader is inexpensive over Scanner in this case. Refer reasons to consider using BufferedReader.

    public static void main(String[] args) throws IOException {
            System.out.println("------\nProgram for calculating sum and average of integers\n------");
            System.out.print("Enter space separated integer values: ");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String[] split = reader.readLine().split("\\s");//splits string on whitespaces
            double sum=0, average;
            for (String numberString : split)
                sum += Integer.parseInt(numberString);
            average = sum / split.length;
            System.out.println("Sum: " + sum);
            System.out.println("Average: " + average);
        }
    
0

A simple solution, where the user does not have to enter the number of the numbers first.

public static void main(String[] args) {
    System.out.println("------------------");
    System.out.println("Program for calculating sum and average of an arbitary series of numbers");
    System.out.println("------------------");

    Scanner input = new Scanner(System.in);
    ArrayList<Integer> values = new ArrayList<>();

    System.out.print("Input numbers. Input something else (e.g. enter) if you're finisehd:\n");
    while (true) {
        try {
            values.add(Integer.parseInt(input.nextLine()));
        } catch (Exception e) {
            break;
        }
    }
    System.out.println("Sum is : " + values.stream().mapToInt(Integer::intValue).sum());
    System.out.println("Average is: " + values.stream().mapToInt(Integer::intValue).average().getAsDouble());
}
Rene Knop
  • 1,788
  • 3
  • 15
  • 27