471,616 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // Imported Packages/Classes
  3. import javax.swing.*;
  4.  
  5. public class Program6 {
  6.  
  7. // Add your class methods up here to make the main given to you work correctly!
  8.  
  9.  
  10. public static void putStringIntoArray (String s, char[] cArray) {
  11.  
  12. for (int i = 0; i < s.length(); i++) {
  13.  
  14. cArray[i] = s.charAt(i);
  15. }
  16. }
  17.  
  18. public static int removeDuplicateCharacters (char[] withDups, char[] withoutDups) {
  19.  
  20. int lastIndex = -1;
  21.  
  22. for (int i = 0; i < withDups.length; i++) {
  23.  
  24. if (isCharacterPresent(withDups[i], withoutDups, lastIndex) != true) {
  25. lastIndex++;
  26. withoutDups[lastIndex] = withDups[i];
  27. }
  28. }
  29. return lastIndex;
  30.  
  31. }
  32.  
  33. public static boolean isCharacterPresent (char c, char[] cArray, int lastInUse) {
  34.  
  35. for (int i = 0; i <= lastInUse; i++){
  36.  
  37. if ( cArray[i] == c){
  38. return true;
  39. }
  40. }
  41. return false;
  42.  
  43. }
  44.  
  45. public static int find (char c, char[] cArray, int lastInUse) {
  46. for (int i = 0; i <= lastInUse; i++){
  47. if (cArray[i] == c){
  48. return i;
  49. }
  50. }
  51. return -1;
  52. }
  53.  
  54. public static void sortArray (char[] cArray, int lastInUse) {
  55.  
  56. for (int i = 0; i <cArray.length; i++) {
  57. java.util.Arrays.sort(cArray);
  58. }
  59.  
  60. }
  61.  
  62.  
  63.  
  64. public static String putArrayIntoString (char[] cArray, int lastInUse) {
  65. String someArray = "";
  66.  
  67. for (int i = 0; i <= lastInUse; i++){
  68. someArray += cArray[i];
  69. }
  70. return someArray;
  71.  
  72. }
  73.  
  74. public static String putArrayIntoString (char[] cArray) {
  75. String someArray = "";
  76. for (int i = 0; i < cArray.length; i++){
  77. someArray += cArray[i];
  78. }
  79.  
  80. return someArray;
  81.  
  82.  
  83. }
  84.  
  85. public static void showOutputInJOptionPane (String output) {
  86.  
  87. JScrollPane jsp = new JScrollPane(new JTextArea(output));
  88. jsp.setPreferredSize(new java.awt.Dimension(200, 100));
  89. JOptionPane.showMessageDialog(null, jsp, "Message", JOptionPane.INFORMATION_MESSAGE, null);
  90.  
  91. }
  92.  
  93. public static void main (String[] args) {
  94.  
  95. // Local Variables.
  96. String input = "";
  97. String output = "";
  98. char[] allCharacters;
  99. char[] noRepeatCharacters;
  100. int lastInUse;
  101.  
  102. do {
  103. input = JOptionPane.showInputDialog("Enter a non-empty string");
  104. if (input == null){
  105. JOptionPane.showMessageDialog(null,
  106. "User clicked cancel. The program will exit",
  107. "Program 6 Fall 2007", JOptionPane.PLAIN_MESSAGE);
  108. System.exit(0);
  109. }
  110. else if (input.length() > 0) {
  111. allCharacters = new char[input.length()];
  112. noRepeatCharacters = new char[input.length()];
  113. putStringIntoArray(input,allCharacters);
  114.  
  115. // Start to build the output string!
  116. output = "Original string: \"" + putArrayIntoString(allCharacters) +
  117. "\".\n";
  118.  
  119. lastInUse = removeDuplicateCharacters(allCharacters, noRepeatCharacters);
  120.  
  121. // Add the no-duplicates output
  122. output += "No duplicates version: \"" + putArrayIntoString(noRepeatCharacters,
  123. lastInUse) + "\".\n";
  124.  
  125. sortArray(noRepeatCharacters,lastInUse); // Sorts array itself
  126.  
  127. // Add the no-duplicates sorted version to output
  128. output += "Sorted version: \"" + putArrayIntoString(noRepeatCharacters,
  129. lastInUse) + "\".\n";
  130.  
  131. int where = find('a',noRepeatCharacters,lastInUse);
  132. if (where >= 0)
  133. output += "Found an 'a' in position " + (where+1) + ".\n";
  134. else
  135. output += "Found no a's in the array.\n";
  136.  
  137. where = find('e',noRepeatCharacters,lastInUse);
  138. if (where >= 0)
  139. output += "Found an 'e' in position " + (where+1) + ".\n";
  140. else
  141. output += "Found no e's in the array.\n";
  142.  
  143. where = find('i',noRepeatCharacters,lastInUse);
  144. if (where >= 0)
  145. output += "Found an 'i' in position " + (where+1) + ".\n";
  146. else
  147. output += "Found no i's in the array.\n";
  148.  
  149. where = find('o',noRepeatCharacters,lastInUse);
  150. if (where >= 0)
  151. output += "Found an 'o' in position " + (where+1) + ".\n";
  152. else
  153. output += "Found no o's in the array.\n";
  154.  
  155. where = find('u',noRepeatCharacters,lastInUse);
  156. if (where >= 0)
  157. output += "Found a 'u' in position " + (where+1) + ".\n";
  158. else
  159. output += "Found no u's in the array.\n";
  160.  
  161.  
  162. showOutputInJOptionPane(output);
  163. }
  164. else ; // do nothing he entered a blank line.
  165.  
  166. }while (true);
  167. } // end of main
  168.  
  169. } // end of Program6
  170.  
  171.  
Oct 25 '07 #1
3 2376
r035198x
13,262 8TB
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).
Oct 26 '07 #2
i figured it out thanx for the help guys
Oct 26 '07 #3
r035198x
13,262 8TB
i figured it out thanx for the help guys
That is good news.
Oct 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Federico G. Babelis | last post: by
22 posts views Thread by mike | last post: by
3 posts views Thread by SilverWolf | last post: by
28 posts views Thread by Bailey.W87 | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
6 posts views Thread by Dennis Gearon | last post: by
9 posts views Thread by Dylan Parry | last post: by
77 posts views Thread by arnuld | last post: by
5 posts views Thread by jrod11 | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.