5 Replies Latest reply: Aug 17, 2008 12:52 AM by 843785 RSS

    help with arrays arranging names in reverse alphabetical order!!

    843785
      hi I need help please with this assignment the instructions are these:
      Write the class ArrayTest that contains 3 static methods, main, sort,
      and printArray.
      The first method prints a title, tells the user that it will sort a list of names
      and prompts him/her, through a JOption pane, to enter n, the size of the list. If n
      is less than or equal to one, main will display an error message and exit.
      Otherwise, it declares an array of n strings and enters a loop that reads the names.
      The loop body prompts the user, through a JOption pane, to enter the next name.
      After reading the names, main prints the header
      Unsorted Names
      --------------

      and calls printArray to print the names.
      Then, it calls sort, prints the header
      Sorted Names
      ------------

      and calls printArray to display the sorted array.

      The method
      public static void sort(String[] names)

      sorts the names in reverse alphabetical order, i.e. name1 < name2 if name2 precedes
      name1 in alphabetical order.

      The method
      public static void printArray(String[] names)

      =====================================================================================================
      I could get this program to display alphabetically but I need help to reverse the order and tell me if i'm doing it right

      package test;
      import java.util.Comparator;
      import javax.swing.JOptionPane;
      import java.util.Arrays;
      public class Main {




















      public Main() {


      String input = JOptionPane.showInputDialog("Enter the number" + " of the list of names");
      int a = Integer.parseInt(input);

      if (a <= 1 ) {
      JOptionPane.showMessageDialog(null, "Not enough names!", "Please enter more names!", JOptionPane.ERROR_MESSAGE);
      }

      else if (a > 1){

      String []names = new String [a];







      int i;
      String sort = "";

      // Get names
      for(i = 0; i < names.length; ++i)
      names[i] = (String)JOptionPane.showInputDialog(null, "Please enter a name:");





      // Sort names
      Arrays.sort(names);

      // Join names
      for(i = 0; i < names.length; ++i)
      sort += names[i] + "\n";

      // Output names
      JOptionPane.showMessageDialog(null, sort);
      }




      }

      public static void main(String[] args) {
      new Main();
      }

      }


      thanks