0

Hy. I wrote this piece of code

import java.util.Scanner;
public class SportsEvents {
public static void main(String[] args) {
    String[] contestants = getcontestants();
    int[] score=getscores();
    System.out.println("The maximum score is: "+getMaxValue(score));
    System.out.println("The minimum score is: "+getMinValue(score)); 
    System.out.println("The average is: "+getAverage(score));
}
public static String[] getcontestants()
{
    int numcontestants=1;
    String name[] = new String[numcontestants];

    for(int j=0;j<numcontestants;j++){
        Scanner ip=new Scanner(System.in);
        System.out.println("Enter contestant's name");
        name[j]=ip.nextLine();
    }
    return name;
}
public static int[] getscores()
{
    int numscores=8;
    int score[] = new int[numscores];
    for (int a=0;a<numscores;a++){
        Scanner ip=new Scanner(System.in);
        System.out.println("Enter the scores");
        score[a]=ip.nextInt();
    }
    return score;
}
public static int getMaxValue(int[] array) {
    int maxValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > maxValue) {
            maxValue = array[i];
        }
    }
    return maxValue;
}
public static int getMinValue(int[] array) {
    int minValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] < minValue) {
            minValue = array[i];
        }
    }
    return minValue;
}
public static int getAverage(int[] people) {
   int sum = 0;
   for (int i=0; i < people.length; i++) {
        sum = sum + people[i];
   }
   int result = sum / people.length;
   return result;
}
}

As you can see, the user enters a name and 8 scores and the computers find the biggest score, the lowest score, and the average. I want a non-void method that will give a summary of everything but I don't know how to do it. Ex: If I entered James as a name and the numbers 1-8, I want it to print: The contestant is James, his highest score was 8. His lowest score was 1. His average is 49. Thank you and tell me if it's confusing!!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Do you mean, you need to write one method that'll return all the info? – Thiyagu May 12 '18 at 05:09
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Amadou Toure May 12 '18 at 19:52

1 Answers1

2

What you want is to store your immediate result into a single object contains min, max, avg.

In java 8 there's already a method out of the box for you:

int[] score=getscores();
IntSummaryStatistics stats = Arrays.stream(score).summaryStatistics();

IntSummaryStatistics contains

private long count;
private long sum;
private int min;
private int max;

You can also manually get that result:

First define a class will hold your data:

public class SportsEvents {

   public static class IntArrayStatistic {
        private int min = Integer.MAX_VALUE;
        private int max = Integer.MIN_VALUE;
        private int sum = 0;
        private int avg;
        private int count;

        // Getter - Setter

        public static IntArrayStatistic of(int[] input) {
            if (input == null) {
                throw new IllegalArgumentException("Null array");
            }
            IntArrayStatistic result = new IntArrayStatistic();
            result.count = input.length;

            for (int i : input) {
                if (i < result.min) {
                    result.min = i;
                }
                if (i > result.max) {
                    result.max = i;
                }
                result.sum += i;
            }
            result.avg = result.sum / result.count;
            return result;
        }
    }
}

And to use this:

int[] score=getscores();
IntArrayStatistic stats = IntArrayStatistic.of(score);
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51