As mentioned in a comment, I think your whole process smells. You should check for the Single Resposibility Principle (SRP). In essence the principle says:
A method, class or package should only be responsible for exactly one thing. Methods do one task, classes represent one specific object and packages organize one part of your application.
Now if we look at your code and see what the main method does, we see it is responsible for:
reading input, "keeping" it, calculating average, max and min, outputting results
Now let's split that up into methods, that are responsible for a single thing. I suggest you keep the data in the main-method.
Other minor and major things:
String concatenation and formatting:
"The numbers you have entered are:\n" + display + "\nSum of all numbers is: " + total + "\nAverage is: " + average + "\nMinimum number is: " + min + "\nMaximum number is: " + max
This is the complicated way to do things. More java-like would be to use the String.format
-method:
String.format("The numbers you have entered are:\n %s"
+ "\n Sum of all numbers is: %d\nAverage is: %d"
+ "\nMinimum number is: %d\nMaximum number is:%d",
numbers.toString(), total, average, min, max);
Reusing the analysis result:
It's difficult to pass around the analysis result if you'd need it in some other place, than as Strings in your output. Thus I'd recommend using an inner class to store the result. In that class you should also do the String operations and stuff.
If you need the results somewhere else, you can easily make the inner class a "public" one and move it out of your ArrayTajba
. Then you can use it "anywhere" ;)
Magic numbers:
5 is a magic number in your code. What if you want to have 7 numbers instead? I suggest extracting it to a named constant.
Rewriting your code:
public class ArrayTajba {
private static final int NUMBER_COUNT = 5;
public static void main(String[] args) {
int[] numbers = readUserInput(NUMBER_COUNT);
AnalysisResult results = AnalysisResult.analyze(numbers);
outputToUser(results.toString());
}
private static int[] readUserInput(final int inputCount) {
int[] results = new int[inputCount];
for(int i = 0; i < results.length; i++){
String userStringInput = JOptionPane.showInputDialog(/* shortcutting here */);
results[i] = Integer.parseInt(userStringInput);
}
return results;
}
private static void outputToUser(final String message){
JOptionPane.showMessageDialog(null, message, JOptionPane.INFORMATION_MESSAGE);
}
private final class AnalysisResult {
private int[] numbers;
private int max;
private int min;
private int total;
private double average;
public static AnalysisResult analyze(final int[] numbers) {
AnalysisResult result = new AnalysisResult(numbers);
result.runCalculations();
return result;
}
private AnalysisResult(final int[] inputArray) {
//prevent changes from outside
numbers = inputArray.clone();
}
private void runCalculations() {
min = numbers[0];
max = numbers[0];
total = 0;
for (int number : numbers) {
if(number < min) {
min = number;
}
if(number > max) {
max = number;
}
total += number;
}
average = total / (double) number.length;
}
public String toString() {
return String.format("The numbers you have entered are:\n %s"
+ "\n Sum of all numbers is: %d\nAverage is: %d"
+ "\nMinimum number is: %d\nMaximum number is:%d",
numbers.toString(), total, average, min, max);
}
}