Results 1 to 3 of 3

Thread: Array sort

  1. #1
    Jeremy720 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default 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);
    }


    }

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    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.

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  2. How to sort an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:48 PM
  3. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 02:11 AM
  4. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 07:56 AM
  5. Help with sort algorithm
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •