How do I go about defining the average and total variables in the following script? Assistance is appreciated... Ben

import javax.swing.JOptionPane;

class minmax {
/** Main Method */
public static void main(String[] args)
{
final double TOTAL_NUMBERS = 5;

//Convert string to double
double Average = Double.parseDouble(averageString);

//Convert string to double
double total = Double.parseDouble(totalString);

// Prompt the user to enter a double value
for (int i =0; i < numbers.length;i++) {
String doubleString = JOptionPane.showInputDialog(null,
"Enter a double value","Input (double)",
JOptionPane.QUESTION_MESSAGE);
}

// Find the largest
int doubles = numbers[0];
for (int i = 1; i < doublenumbers.length; i++) {
if (doubles < numbers)
doubles = doublenumbers;
}

// Find the smallest
int doubles = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (doubles < numbers)
doubles = numbers;
}

// Prepare the results
String output = "The values are ";
for (int i = 0; i < numbers. length; i++) {
output += numbers + " ";
}

output += "\nThe largest number is " + doubles;
output += "\nThe smallest number is " + doubles;
output += "\nThe average number is " + average;
output += "\nThe total number is " + total;

// Display results
JOptionPane.showMessageDialog(null,doublevalue +
"Output", JOptionPane.INFORMATION_MESSAGE);
}
}

## All 3 Replies

for a start tidy your code up... declare all the variables at the top before even using them, and only create them when u think its really necessary... also you cant create a variable with the same name as a previously created variable. I also suggest on looking at how to declare array variables.

for a start tidy your code up... declare all the variables at the top before even using them

No, don't declare all your variables at the top. Declare them at the beginning of their scope.
Eg:

``````void myMethod() {
int int1 = 0;         // beginning of method block

if (condition) {
int int2 = 0;     // beginning of "if" block
...
}
}``````

@ben1977
I would also recommend not using a String for your output variable. You are constantly concatenating to the string, causing many memory allocations. Use a StringBuilder or StringBuffer instead.

``````StringBuilder builder = new StringBuilder("This ");
builder.append("is a");
builder.append(" StringBuffer");
System.out.println(builder);``````

:o errrm ... yeah i wasnt being too specific at all wasnt i....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.