Results 1 to 7 of 7

Thread: Sort Problem

  1. #1
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Sort Problem

    Hey guys,
    I'm trying to solve a problem in which the user keeps inserting numbers and everytime the user inserts a number the problem should sort the array.

    This is the code I've done :
    Java Code:
    public class ex5
    {
        public static void main(String Args[]){
            Scanner input = new Scanner(System.in);
            System.out.println("Quantos inteiros quer ler?");
            int n_inteiros = input.nextInt();
            int[] a = new int[n_inteiros];
            for(int i=0;i<n_inteiros;i++){
            a[i]=0;
            }
            int i=0;
            while(i<n_inteiros){
                int novo= input.nextInt();
                a[i]=novo;
                Arrays.sort(a);
                i++;
            }
            System.out.println("Estado final do array:");
            for(int j=0;j<n_inteiros;j++){
                System.out.println(a[j]);
            }
        }
    }
    Now the output :
    Java Code:
    Quantos inteiros quer ler?
    3
    1
    2
    3
    Estado final do array:
    0
    1
    3
    Any help please ?
    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Sort Problem

    Try adding a print out of the array after every sort to see what is in it. Use the Arrays toString() method to format the array:
    S.o.println(Arrays.toString(a));

    This will help you understand what the code is doing.

  3. #3
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Sort Problem

    Thanks Norm.
    What's the S.o. for ?
    I'm still a beginner :/

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Sort Problem

    Sorry. S.o. should be System.out.

  5. #5
    JeremyLin is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Sort Problem

    Wow, that's amazing.
    Thanks a lot Norm.

    Java Code:
    1
    [0, 0, 1]
    2
    [0, 1, 2]
    3
    [0, 1, 3]
    That's what it does.
    Basically it works until the 3, when we hit the three it replaces the last int for the 3 :o
    I have no idea why though :/

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Sort Problem

    Take a piece of paper and draw the array and set the value of i. change the array's contents at i and sort the array and increment i.
    Do it again. Do you ever put an item into the array where there is currently another number? If so you replace that number.

    Work through your code playing comuter with paper and pencil and you will see what it is doing.


    when we hit the three it replaces the last int for the 3
    What is the value of i?

  7. #7
    waiheng1986 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Sort Problem

    I think you should sort the integer array after the integer from user is been inserted into the integer array.
    This should be sort correctly everytime you print it at the end.

    Hope this can help you.

Similar Threads

  1. problem with my bucket sort
    By someone in forum New To Java
    Replies: 8
    Last Post: 04-12-2011, 12:09 AM
  2. Problem with selection sort
    By Metastar in forum New To Java
    Replies: 6
    Last Post: 10-21-2010, 03:18 AM
  3. Radix Sort Problem !!!!
    By javanew in forum Advanced Java
    Replies: 2
    Last Post: 09-21-2010, 05:33 AM
  4. problem with insertion sort???
    By blueduiker in forum New To Java
    Replies: 2
    Last Post: 03-22-2010, 02:17 PM
  5. Problem with sort method
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 08:12 PM

Posting Permissions

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