Results 1 to 3 of 3
Thread: Array sort
 10072008, 09:09 PM #1Member
 Join Date
 Oct 2008
 Posts
 13
 Rep Power
 0
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);
}
}
 10072008, 09:45 PM #2Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 6
Please use code tags; [code][/ code](without spaces) they make it much easier to read.
I die a little on the inside...
Every time I get shot.
 10072008, 11:41 PM #3
Similar Threads

How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04292008, 08:04 PM 
How to sort an array
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:48 PM 
sort
By Camden in forum New To JavaReplies: 7Last Post: 11282007, 01:11 AM 
how to sort
By Feng in forum New To JavaReplies: 1Last Post: 11202007, 06:56 AM 
Help with sort algorithm
By zoe in forum New To JavaReplies: 1Last Post: 08072007, 06:09 AM
Bookmarks