# Array sort

Printable View

• 10-07-2008, 09:09 PM
Jeremy720
Array sort
Hello, I'm new to this and am trying to sort an array from user input. It sorts it if you fill the entire array, but adds two zeros as the first two elements before sorting if the user types 'q' before filling the whole array. I'm sure its a simple mathmatical mistake, but I just can't see it.
Here's my code:

import java.lang.*;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Arrays;

public class ArrayExcercise3
{

public int[] numbers = new int[5];
public int count = 0;
DecimalFormat myFormat = new DecimalFormat("#,##0.00");

public static void main (String [] args) throws IOException
{
ArrayExcercise3 app = new ArrayExcercise3();
app.doProcess();
}

public void doProcess() throws IOException
{
int i = 0;

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

BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));

System.out.println("Enter up to 20 integers; Press \"Q\" to exit");

String str = br.readLine();

if (str.equals("q") || str.equals("Q"))
{
if (i <= 0)
{
System.out.println("You give me nothing to work with!");
System.exit(0);
}

display (i);

System.out.println("You've entered " + i + " numbers");

sum (i);

average(i);

max(i);

min(i);

System.out.println("Thanks for participating!");

System.exit(0);

}

try
{
Integer.parseInt(str);

}

catch (NumberFormatException e)
{
System.out.println("You haven't entered a valid number");
i--;

continue;
}

numbers[i] = Integer.parseInt(str);
}

display(i);

System.out.println("You've entered " + i + " integers");

sum (i);

average(i);

max(i);

min(i);

System.out.println("Thanks for participating!");

System.exit(0);

}

public void display(int i)
{

for(int j = 0; j < i; j++)
{

if ((j + 1) % 5 == 0)
{

Arrays.sort(numbers);
System.out.println (numbers[j] + ",");

}
else

Arrays.sort(numbers);
System.out.print(numbers[j] + ",");

}
}

public void sum(int i)
{
int sum = 0;

for (int j = 0; j < i; j++)
{
sum += numbers[j];
}
int total = sum;
System.out.println("The sum of your numbers is " + total);

}

public void average(int i)
{
DecimalFormat myFormat = new DecimalFormat("#,##0.00");
double average = 0;

int sum = 0;

for(int j = 0; j < i; j++)
{
sum += numbers[j];
average = (sum/i);

if (i <= 0)
{
System.out.println("Thanks for participating!");

System.exit(0);
}

else
{
average = (sum / i);
}

}

System.out.println("The average of your numbers is " +
myFormat.format(average));
}

public void max(int i)
{
int largest = 0;

for(int j = 0; j < i; j++)
{
if (numbers[j] > largest)
largest = numbers[j];
}

System.out.println("The largest number you entered is " + largest);
}

public void min(int i)
{
int smallest = numbers[0];

for(int j = 1; j < i; j++)
{
if (numbers[j] < smallest)
smallest = numbers[j];
}

System.out.println("The smallest number you entered is " + smallest);
}

}
• 10-07-2008, 09:45 PM
Supamagier
Please use code tags; [code][/ code](without spaces) they make it much easier to read.
• 10-07-2008, 11:41 PM
Norm
Quote:

I just can't see it.
Try debugging the code by adding some println() statements to show how variables are changing and the logic flow.
You'll be able to "see it" when you look at the output generated.