Sorting character array help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powerej
    New Member
    • Sep 2007
    • 6

    Sorting character array help

    im having trouble printing an array with duplicate characters, i can enter and print with single characters but when i try and enter and print duplicate ones it puts blanks in my output for a sorted array.

    Code:
    // Imported Packages/Classes
    import javax.swing.*;
    
    public class Program6 {
    
    // Add your class methods up here to make the main given to you work correctly!
    
    
    public static void putStringIntoArray (String s, char[] cArray) {
    
    for (int i = 0; i < s.length(); i++) {
    
    cArray[i] = s.charAt(i);
    }
    }
    
    public static int removeDuplicateCharacters (char[] withDups, char[] withoutDups) {
    
    int lastIndex = -1;
    
    for (int i = 0; i < withDups.length; i++) {
    
    if (isCharacterPresent(withDups[i], withoutDups, lastIndex) != true) {
    lastIndex++;
    withoutDups[lastIndex] = withDups[i];
    }
    }
    return lastIndex;
    
    }
    
    public static boolean isCharacterPresent (char c, char[] cArray, int lastInUse) {
    
    for (int i = 0; i <= lastInUse; i++){
    
    if ( cArray[i] == c){
    return true;
    }
    }
    return false;
    
    }
    
    public static int find (char c, char[] cArray, int lastInUse) {
    for (int i = 0; i <= lastInUse; i++){
    if (cArray[i] == c){
    return i;
    }
    }
    return -1;
    }
    
    public static void sortArray (char[] cArray, int lastInUse) {
    
    for (int i = 0; i <cArray.length; i++) {
    java.util.Arrays.sort(cArray);
    }
    
    }
    
    
    
    public static String putArrayIntoString (char[] cArray, int lastInUse) {
    String someArray = "";
    
    for (int i = 0; i <= lastInUse; i++){
    someArray += cArray[i];
    }
    return someArray;
    
    }
    
    public static String putArrayIntoString (char[] cArray) {
    String someArray = "";
    for (int i = 0; i < cArray.length; i++){
    someArray += cArray[i];
    }
    
    return someArray;
    
    
    }
    
    public static void showOutputInJOptionPane (String output) {
    
    JScrollPane jsp = new JScrollPane(new JTextArea(output));
    jsp.setPreferredSize(new java.awt.Dimension(200, 100));
    JOptionPane.showMessageDialog(null, jsp, "Message", JOptionPane.INFORMATION_MESSAGE, null);
    
    }
    
    public static void main (String[] args) {
    
    // Local Variables.
    String input = "";
    String output = "";
    char[] allCharacters;
    char[] noRepeatCharacters;
    int lastInUse;
    
    do {
    input = JOptionPane.showInputDialog("Enter a non-empty string");
    if (input == null){
    JOptionPane.showMessageDialog(null,
    "User clicked cancel. The program will exit",
    "Program 6 Fall 2007", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);
    }
    else if (input.length() > 0) {
    allCharacters = new char[input.length()];
    noRepeatCharacters = new char[input.length()];
    putStringIntoArray(input,allCharacters);
    
    // Start to build the output string!
    output = "Original string: \"" + putArrayIntoString(allCharacters) +
    "\".\n";
    
    lastInUse = removeDuplicateCharacters(allCharacters, noRepeatCharacters);
    
    // Add the no-duplicates output
    output += "No duplicates version: \"" + putArrayIntoString(noRepeatCharacters,
    lastInUse) + "\".\n";
    
    sortArray(noRepeatCharacters,lastInUse); // Sorts array itself
    
    // Add the no-duplicates sorted version to output
    output += "Sorted version: \"" + putArrayIntoString(noRepeatCharacters,
    lastInUse) + "\".\n";
    
    int where = find('a',noRepeatCharacters,lastInUse);
    if (where >= 0)
    output += "Found an 'a' in position " + (where+1) + ".\n";
    else
    output += "Found no a's in the array.\n";
    
    where = find('e',noRepeatCharacters,lastInUse);
    if (where >= 0)
    output += "Found an 'e' in position " + (where+1) + ".\n";
    else
    output += "Found no e's in the array.\n";
    
    where = find('i',noRepeatCharacters,lastInUse);
    if (where >= 0)
    output += "Found an 'i' in position " + (where+1) + ".\n";
    else
    output += "Found no i's in the array.\n";
    
    where = find('o',noRepeatCharacters,lastInUse);
    if (where >= 0)
    output += "Found an 'o' in position " + (where+1) + ".\n";
    else
    output += "Found no o's in the array.\n";
    
    where = find('u',noRepeatCharacters,lastInUse);
    if (where >= 0)
    output += "Found a 'u' in position " + (where+1) + ".\n";
    else
    output += "Found no u's in the array.\n";
    
    
    showOutputInJOptionPane(output);
    }
    else ; // do nothing he entered a blank line.
    
    }while (true);
    } // end of main
    
    } // end of Program6
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Why don't you explain step by step what the code is supposed to be doing and what it's currently doing( or not doing).

    Comment

    • powerej
      New Member
      • Sep 2007
      • 6

      #3
      i figured it out thanx for the help guys

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by powerej
        i figured it out thanx for the help guys
        That is good news.

        Comment

        Working...