0

i cant sort it descending?
why? please help me!!

import java.io.*;
import java.util.Arrays;
import java.util.Collections;

public class alpha {

    public static void main(String args[]) throws IOException
    {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

int num = 0,c,x,a = 0,d,e,letter1,f;
String input;

System.out.println("\t[WELCOME]");

for (d = 0;d<=2;d++)
{
        System.out.print("How many NUMBERS you want to input? ");
        input = br.readLine();
        try {
            a = Integer.parseInt(input);
            System.out.println("\tYou will enter "+a+" Numbers.");
            break;
        }
        catch(NumberFormatException nfe)
        {
            System.out.println("**Invalid Input!**");
        }
                if (d == 2)
                {
                    System.out.println("Youve reached the maximum trials.");
                    System.out.println("System will exit.");
                    System.exit(0);
                }
}

int number [] = new int [a];
String number1;
String numbdes [] = new String [a];
System.out.println();


System.out.println("\t-Enter your Number-");


for (c = 0;c< a;c++)
   {
      num++;
      System.out.print(num+". ");
      number1 = br.readLine();
       try
       {
        x = Integer.parseInt(number1);
        number [c] = x;
        System.out.println("Integet input!");
       }

        catch(NumberFormatException nfe)
            {
            System.out.println("Not an Integer");
            System.out.println("System will exit.");
            System.exit(0);
            }
   }

    for (e = 0;e<=2;e++)
        {
        System.out.print("\tDo you want to sort?[Y/N]: ");
        String yn = br.readLine();

           if (yn.equalsIgnoreCase("y"))
           {
                        System.out.println();
                        System.out.println("\tYou entered:");
                        for (c = 0;c< a;c++)
                        {
                        System.out.print("\t"+number [c]+", ");
                        }
                        Arrays.sort(number);
                        System.out.println();
                        System.out.println("\tAscending Order:");
                        for (c = 0;c< a;c++)
                        {
                        System.out.print("\t"+number [c]+", ");
                        }
                        System.out.println();
                        Arrays.toString(number);
                        Arrays.sort(number, Collections.reverseOrder());
                        System.out.println("\tDescending Order");
                        for(c= 0;c<a;c++)
                        {
                        System.out.print(number[c]+", ");
                        }
                        System.out.println();
                        System.exit(0);
            }
            else if (yn.equalsIgnoreCase("n"))
            {
                        System.out.println();
                        System.out.println("\tYou entered:");
                        for (c = 0;c< a;c++)
                        {
                        System.out.println("\t"+number [c]+". ");
                        }
                        System.exit(0);
            }
            else
            {
                        System.out.println("\tInvalid Input!");
            }
        if (e == 2)
        {
            System.out.println();
            System.out.println("\tThree consecutives invalid input.");
            System.out.println("\tSystem will exit.");
            System.out.println();
            break;
        }
         }//end for2
    }//main
}//class
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by adams161
0

what error do you get?

i see sample code on the web where something like this is done,

Comparator<Integer> r = Collections.reverseOrder();

you just feed your sort method Collections.reverseOrder(). Do you need to defined the actual object like in my code snippet above, to tell it number is <int>?

Mike

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.