Results 1 to 3 of 3
  1. #1
    larson1118 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Sorting Array Issue

    What I am trying to do is take the encrypted string and count the occurrence of each character in the string. See code below:



    public class Lab9 {

    public static void main(String [] args ) {

    String s = "";

    for (char i = 'A'; i <= 'Z'; i++)
    {

    s += i;

    }
    int [] counter = new int[ s.length() ];

    String encrypted = "FOXRTY YEAWXASCOIKWYATY AITMYR YAFP NXYTSCOI AT POIYTSWB AIH FOVKWYSYWB AT KOTTCQWY BOXR AITMYRT MCWW PYWK VY CVKROEY VB SYAFPCIG AIH SPY FOXRTY FXRRCFXWXV BOXR RYTKOITYT ARY AIOIBVOXT AIH C MCWW IOS TYY SPYV XISCW AUSYR UCIAW GRAHYT PAEY QYYI GCEYIPOM MOXWH BOX RASY BOXR WYEYW OU LIOMWYHGY AIH YDKYRCYIFY OU SPY TXQJYFS VASSYR QYUORY SALCIG SPCT FOXRTY CY SOSAW QYGCIIYR TOVYMPAS YDKYRCYIFYH EYRB YDKYRCYIFYH YDKYRSPOM MOXWH BOX RASY BOXR EYW OU LIOMWYHGY AIH YDKYRCYIFY OU SPY TXQJYFS VASSYR AUSYR SALCIG SPCT FOXRTYMPAS MAT SPY VOTS CVKORSAIS SPCIG BOX WYARIYH CI SPCT FOXRTYMPAS HCH BOX WCLY QYTS AQOXS SPY FOXRTYMPAS HCH BOX WCLY WYATS AQOXS SPY FOXRTYMPCFP AFSCECSCYT CY WYFSXRYT WAQT KROGRAVT SYTST YSF MYRY PYWKUXW SO BOXR WYARICIGMPCFP AFSCECSCYT MYRY IOS PYWKUXW SO BOXPOM FAI SPY FOXRTY QY CVKROEYHAHHCSCOIAW FOVVYIST";

    for (int i = 0; i < encrypted.length(); i++)
    {
    int index = s.indexOf(encrypted.charAt(i));

    if(index > -1) counter[index] ++;

    }
    for (int i = 0; i < counter.length; i++)
    {
    if(counter[i] >= 0)

    System.out.printf("%2s - %2d%n",s.charAt(i), counter[i]);

    }
    }
    }

    This is working just fine. I get an accurate count of the letter occurrences. Here is the output:

    A - 46
    B - 16
    C - 43
    D - 5
    E - 10
    F - 27
    G - 11
    H - 20
    I - 39
    J - 2
    K - 16
    L - 6
    M - 20
    N - 1
    O - 54
    P - 30
    Q - 11
    R - 44
    S - 58
    T - 42
    U - 10
    V - 16
    W - 34
    X - 35
    Y - 104
    Z - 0


    What I would like to do is now sort the output by occurrences rather than character itself. It tried using the Arrays.sort method but all that is doing is sorting the occurrences and leaving the characters in the same order. See below:

    A - 0
    B - 1
    C - 2
    D - 5
    E - 6
    F - 10
    G - 10
    H - 11
    I - 11
    J - 16
    K - 16
    L - 16
    M - 20
    N - 20
    O - 27
    P - 30
    Q - 34
    R - 35
    S - 39
    T - 42
    U - 43
    V - 44
    W - 46
    X - 54
    Y - 58
    Z - 104


    Any help would be appreciated.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    Don't make your 'counter' array an int array, make it an array of small objects like this:

    Java Code:
    class CountObject implements Comparable<CountObject> {
       private char c;
       private int count;
    
       public CountObject(char c) { this.c= c; }
    
       public void inc() { count++; }
    
       public int compareTo(CompareObject that) {
    
          return this.count-that.count;
       }
    
       public String toString() { return c+": "+count; }
    }
    The object carries that character it is counting around and it can compare itself against another CountObject, so it can be sorted. Sort the array of CountObjects and see the results.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    Hi Josah,
    Not that I disagree with your code, it was actually the way I was going to prescribe. I just have a few questions.

    Java Code:
    class CountObject implements Comparable<CountObject> {
       private char c;
       private int count;
    
       public CountObject(char c) { this.c= c; }
    
       public void inc() { count++; }
    
       public int compareTo(CompareObject that) {
    
          return this.count-that.count;
       }
    
       public String toString() { return c+": "+count; }
    }

    the state variable count, which you have declared as an int, is not initialised in the constructor; I'm assuming you are ignoring this step as int's are always 0 by default when declared. In general, is this okay to do in general? I agree it works, I'm just curious as to whether you would recommend it in general practice?

    Again, let me reiterate, not criticizing, just curious.

    Cheers,

    David

Similar Threads

  1. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 01:50 AM
  2. Array List Sorting
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 11-14-2010, 02:33 AM
  3. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:42 AM
  4. Sorting an Array via Stacks
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 02-01-2010, 06:53 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 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
  •