Results 1 to 7 of 7
  1. #1
    Freeze69 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Question Help with sort ascending and descending

    Can someone please help me with my code.. im kinda stuck in ascending and descending.. here is my code hope you can understand it.. it just need a little of your magic trick so it could arrange the numbers correctly! Thanks in advance!



    Java Code:
    import java.io.*;
    
    public class Sorting {
        public static void main(String[]args)throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int [] x;
            x = new int [10];
            int y;
            
            try{
                System.out.println("Please enter 10 numbers: ");
                x[0]=Integer.parseInt(br.readLine());
                x[1]=Integer.parseInt(br.readLine());
                x[2]=Integer.parseInt(br.readLine());
                x[3]=Integer.parseInt(br.readLine());
                x[4]=Integer.parseInt(br.readLine());
                x[5]=Integer.parseInt(br.readLine());
                x[6]=Integer.parseInt(br.readLine());
                x[7]=Integer.parseInt(br.readLine());
                x[8]=Integer.parseInt(br.readLine());
                x[9]=Integer.parseInt(br.readLine());
                
                System.out.println("Your numbers are: "+x[0]+" "+x[1]+" "+x[2]+" "+x[3]+" "+x[4]+" "+x[5]+" "+x[6]+" "+x[7]+" "+x[8]+" "+x[9]);
                System.out.println("[1] Sort ascending");
                System.out.println("[2] Sort descending");
                System.out.println("Choose what type of sort: ");
                y=Integer.parseInt(br.readLine());
                int j=0,i=0, value = 0, n= x.length;
              try{
                   if (y==1){
                        for(i = 1; i<n;i++){
                           value = x[i];
                           j=i;
                        while((j>0)&&(x[j-1]>value)){
                           x[i] = x [j=1];
                           j = j-1;
                   }
                
                        x[j] = value;
                        System.out.println("In ascending order");
                        for(i=0; i<n; i++)
                        System.out.println(x[i]+" ");
                
                   }
                   }else if(y==2){
                        for(i = 1; i>n;i++){
                        value = x[i];
                         j=i;
                     while((j>0)&&(x[j-1]>value)){
                         x[i] = x [j=1];
                         j = j-1;
               
                   }
                     x[j] = value;
                     System.out.println("In descending order");
                     for(i=0; i>n; i++)
                     System.out.println(x[i]+" ");
                   }
                 
                   }else{
                       System.out.println("Error! choices are 1 and 2 only");
                   }
              }catch(NumberFormatException nfe){
                  System.out.println("Invalid Input! Please try again");
              }
            }catch(NumberFormatException nfe){
                System.out.println("Invalid Input! Please try again");
            }
        }
        
    }
    Last edited by Freeze69; 08-16-2011 at 05:26 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Please use code tags when posting code (use the # button in the message toolbar).
    Unformatted code is hard to follow.

    Also, what is your problem?
    You haven't actually said...

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    it just need a little of your magic trick so it could arrange the numbers correctly
    Integer[] x = new Integer[10];
    ....
    if (y == 1) {
    Arrays.sort(x);
    } else if (y == 2) {
    Arrays.sort(x , Collections.reverseOrder());
    }


  4. #4
    Freeze69 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    sorry mate. .its my first time posting a thread here in java-forums.. anyway my problem is here..

    Java Code:
    int j=0,i=0, value = 0, n= x.length;
              try{
                   if (y==1){
                        for(i = 1; i<n;i++){
                           value = x[i];
                           j=i;
                        while((j>0)&&(x[j-1]>value)){
                           x[i] = x [j=1];
                           j = j-1;
                   }
                
                        x[j] = value;
                        System.out.println("In ascending order");
                        for(i=0; i<n; i++)
                        System.out.println(x[i]+" ");
                
                   }
                   }else if(y==2){
                        for(i = 1; i>n;i++){
                        value = x[i];
                         j=i;
                     while((j>0)&&(x[j-1]>value)){
                         x[i] = x [j=1];
                         j = j-1;
               
                   }
                     x[j] = value;
                     System.out.println("In descending order");
                     for(i=0; i>n; i++)
                     System.out.println(x[i]+" ");
                   }
                 
                   }else{
                       System.out.println("Error! choices are 1 and 2 only");
                   }
    my code is that the user will give 10 numbers then he'll choose if he like to arrange it by ascending and descending order..
    as you can see.. its in disorder but it runs smoothly.. Im stuck in this part right here and i don't have a clue to what ill do next, i think my problem is in the computation cause it wont arrange the 10 numbers in ascending and descending order..

  5. #5
    Freeze69 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    Integer[] x = new Integer[10];
    ....
    if (y == 1) {
    Arrays.sort(x);
    } else if (y == 2) {
    Arrays.sort(x , Collections.reverseOrder());
    }

    dude.. i already know that.. we do it the hard way.. thanks anyway

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Then read Sorting algorithm - Wikipedia, the free encyclopedia, choose an algorithm, read, code, ask quations again :D

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Or you could try debugging your code to see where the problem is. Add printlns to show the values of variables at the point where you are making decisions and changing the order of the numbers in the array.

    Some suggestions for easier debugging:
    Hard code the numbers in an array instead of asking a user to enter them:
    int[] x = {3,5,1,7,22,6}; // define the numbers to sort

Similar Threads

  1. Sorting in ascending and descending order
    By flpanthers1 in forum New To Java
    Replies: 10
    Last Post: 06-27-2011, 03:48 PM
  2. ascending order using array rush
    By jca in forum New To Java
    Replies: 2
    Last Post: 01-03-2011, 04:24 AM
  3. Checking ascending order of array
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 10:44 PM
  4. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 04:31 AM
  5. Descending order
    By santanu in forum New To Java
    Replies: 1
    Last Post: 11-04-2008, 04:33 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
  •