473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 2554
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

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

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
28
by: Bailey.W87 | last post by:
my professor give me this assignment. Sort the R's B's and W's in an array. for example, the user enter: R B W W B B R W W R R W R B W i need to swap the characters in the array and arrange it...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
6
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.