Ordering an array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 16
Reputation: Zonr_0 is an unknown quantity at this point 
Solved Threads: 3
Zonr_0 Zonr_0 is offline Offline
Newbie Poster

Ordering an array

 
0
  #1
Oct 24th, 2007
Hi! Haven't posted on danniweb for awhile, but I'm hoping I can get some help. I am in the middle of learning Java, which will be my second language after python, and I have come across a problem, ordering a list of integers in an array.

In python, there's a simple order function that will organize a list by integer or character code (effectively alphabatizing a list of strings, provided they all start with the same case), but as far as I can tell, there's no method in the java API to order an array of ints.

This is what I have so far, it is part of a larger program, a timer that will keep track of the top five longest times recorded. TextIO is an external class written by David Eck, author of one of the books I am teaching myself from. There are several errors with this current version, with some things changing to seemingly arbitrary numbers. The other big thing, more evident in the rest of the program, is that I believe I have violated a good deal of good practice and coding conventions.

  1. public static void recordUpdate(int spoken){
  2. int[] tempArray = new int[5];
  3. TextIO.readFile("records.txt");
  4.  
  5.  
  6. for(int i = 0; i < 5; i++){
  7.  
  8. tempArray[i] = TextIO.getlnInt();
  9. }// end for
  10. for(int i = 0; i < 5; i++){
  11. if (spoken >= tempArray[i]){ //if new number is greater than a number in the records
  12. for(int j = 0; j < (tempArray.length - i - 1); j++){
  13. tempArray[j] = tempArray[j + 1];
  14. }//end for
  15. tempArray[i] = spoken;
  16. break;
  17. }//end if
  18. /*if (i == tempArray[i-1]){
  19.   break;}
  20. not sure where I was going here, left it for a day forgot to comment it, came back to it and couldn't remember my point. Was something to do with one of the for loops needing to break to avoid something screwy.
  21.   */
  22.  
  23. }//end for
  24. GUITimer.records = tempArray.clone(); //copies the temporary array into the variable used by the rest of hte program
  25. for(int i = 0; i < 5; i++){
  26. TextIO.putln(GUITimer.records[i]);
  27. }//end for
  28. }
The code is supposed to go through the array, then stop if it finds a number less then the value the clock was stopped at. Then it attempts to 'bump' everything in the array from that point forward down a spot, then finnally override the stopping spot with the new value. I haven't found a way to get rid of the last integer in the array.

My friend and I have been puzzling over this problem for awhile, so any help on how to create a for loop to order integers would be appreciated. Code for the rest of the program follows, keep in mind that the timer is not very accurate, but it's accurate enough for it's purposes.

edit: code has had a few in-joke variables changed for clarity while posting, so there may have been some accidental errors that aren't normally in the program. Remember you will need the TextIO class for it to compile and run correctly.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.font.FontRenderContext;
  4. import java.awt.geom.Rectangle2D;
  5. import javax.swing.*;
  6.  
  7. public class GUITimer
  8. {
  9. static boolean isRecording = true;
  10. static int timeSpoken = 0;
  11. static final boolean moreMagic = true;
  12. static int[] records = new int[5];
  13. public static void main(String[] args)
  14. {
  15. CenteredFrame frame = new CenteredFrame();
  16.  
  17. //TextIO.readFile("records.txt");
  18. recordUpdate( timeSpoken );
  19.  
  20. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. if (moreMagic){
  22. frame.show();
  23. }
  24. }
  25.  
  26. public static void recordUpdate(int spoken){
  27. int[] tempArray = new int[5];
  28. TextIO.readFile("records.txt");
  29.  
  30.  
  31. for(int i = 0; i < 5; i++){
  32.  
  33. tempArray[i] = TextIO.getlnInt();
  34. }// end for
  35. for(int i = 0; i < 5; i++){
  36. if (spoken >= tempArray[i]){ //if new number is greater than a number
  37. for(int j = 0; j < (tempArray.length - i - 1); j++){
  38. tempArray[j] = tempArray[j + 1];
  39. }//end for
  40. tempArray[i] = spoken;
  41. break;
  42. }//end if
  43. /*
  44.   if (i == tempArray[i-1]){
  45.   break;}
  46. */
  47.  
  48. }//end for
  49. GUITimer.records = tempArray.clone();
  50. for(int i = 0; i < 5; i++){
  51. TextIO.putln(GUITimer.records[i]);
  52. }//end for
  53. }
  54.  
  55. }
  56.  
  57. class CenteredFrame extends JFrame
  58. {
  59. public CenteredFrame()
  60. {
  61. setTitle("Timer of sorts");
  62.  
  63. // get screen dimensions
  64. Toolkit kit = Toolkit.getDefaultToolkit();
  65. Dimension screenSize = kit.getScreenSize();
  66. int screenHeight = screenSize.height;
  67. int screenWidth = screenSize.width;
  68.  
  69. // center frame in screen
  70. setSize(screenWidth / 2, screenHeight / 2);
  71. setLocation(screenWidth / 4, screenHeight / 4);
  72.  
  73. PanelStuff panel = new PanelStuff();
  74. Container contentPane = getContentPane();
  75. contentPane.add(panel);
  76. }
  77. }
  78.  
  79. class PanelStuff extends JPanel
  80. {
  81. static int seconds = 0;
  82. static int minutes = 0;
  83. static int hours = 0;
  84. static int timeTemp = 0;
  85.  
  86. public void paintComponent(Graphics g)
  87. {
  88. super.paintComponent(g);
  89. Graphics2D g2 = (Graphics2D)g;
  90.  
  91. String secondsString = Integer.toString(seconds);
  92. String minutesString = Integer.toString(minutes);
  93. String hoursString = Integer.toString((hours));
  94.  
  95. if (seconds < 10)
  96. {
  97. secondsString = "0" + secondsString;
  98. }
  99. if (minutes < 10)
  100. {
  101. minutesString = "0" + minutesString;
  102. }
  103. if (hours < 10)
  104. {
  105. hoursString = "0" + hoursString;
  106. }
  107.  
  108.  
  109. String timeOutput = (hoursString + ":" + minutesString + ":" + secondsString);
  110.  
  111. // The string properties
  112. g.setColor(new Color(250, 80, 0));
  113. String message = timeOutput;
  114. Font f = new Font("Serif", Font.BOLD, 36);
  115. g2.setFont(f);
  116.  
  117. // measure the size of the message
  118. FontRenderContext context = g2.getFontRenderContext();
  119. Rectangle2D bounds = f.getStringBounds(message, context);
  120.  
  121. // set (x,y) = top left corner of text
  122. double x = (getWidth() - bounds.getWidth()) /2;
  123. double y = (getHeight() - bounds.getHeight())/2;
  124.  
  125. //add ascent to y to reach the baseline
  126. double ascent = -bounds.getY();
  127. double baseY = y + ascent;
  128. g.drawString(message, (int)x, (int)baseY);
  129.  
  130. // temp record test
  131. f = new Font("Serif", Font.BOLD, 15);
  132. g2.setFont(f);
  133. g.drawString(Integer.toString(GUITimer.records[0]) +
  134. Integer.toString(GUITimer.records[1]) +
  135. Integer.toString(GUITimer.records[2]) +
  136. Integer.toString(GUITimer.records[3]) +
  137. Integer.toString(GUITimer.records[4]), 50, 50);
  138. repaint();
  139.  
  140. }
  141. public static void Convert()
  142. {
  143. timeTemp = GUITimer.timeSpoken;
  144. minutes = timeTemp / 60;
  145. seconds = timeTemp % 60;
  146. {
  147. hours = minutes / 60;
  148. minutes = minutes % 60;
  149. }
  150.  
  151. }
  152.  
  153. class CounterAction implements ActionListener{
  154. public void actionPerformed(ActionEvent evt){
  155. if (GUITimer.isRecording == true){
  156. GUITimer.timeSpoken = GUITimer.timeSpoken + 1;
  157. Convert();
  158. }
  159. else
  160. return;
  161. }
  162. }
  163. PanelStuff(){
  164. //DAN was here
  165. ActionListener listener = new CounterAction();
  166. Timer timer = new Timer(1000, listener);
  167. timer.start();
  168.  
  169. JButton startBtn = new JButton("Start/Stop");
  170. JButton resetBtn = new JButton("Reset");
  171.  
  172. add(startBtn);
  173. add(resetBtn);
  174.  
  175. ButtonActionStart startAction = new ButtonActionStart();
  176. ButtonActionReset resetAction = new ButtonActionReset();
  177.  
  178. startBtn.addActionListener(startAction);
  179. resetBtn.addActionListener(resetAction);
  180. }
  181.  
  182. // button stuff
  183. private class ButtonActionStart implements ActionListener
  184. {
  185. public void actionPerformed(ActionEvent evt)
  186. {
  187. if (GUITimer.isRecording == false)
  188. GUITimer.isRecording = true;
  189. else
  190. GUITimer.isRecording = false;
  191. }
  192. }
  193. private class ButtonActionReset implements ActionListener
  194. {
  195. public void actionPerformed(ActionEvent evt)
  196. {
  197. TextIO.readFile("records.txt");
  198. GUITimer.recordUpdate(GUITimer.timeSpoken);
  199.  
  200. GUITimer.timeSpoken = 0;
  201. }
  202. }
  203.  
  204. }
Last edited by Zonr_0; Oct 24th, 2007 at 11:10 pm. Reason: added java to the code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,194
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Ordering an array

 
0
  #2
Oct 25th, 2007
Use class Arrays. This class contains various methods for manipulating arrays.

Info from API on sort(int[] a)

Here is a nice refference to sorting

An here is just small quick example
  1. import java.util.*;
  2.  
  3. public class Sort {
  4. public static void main(String[] args) {
  5. int[] arr = new int[] {5, 3, 1, 6, 7};
  6. Arrays.sort(arr);
  7. for(int i =0; i< arr.length; i++)
  8. {
  9. System.out.print(arr[i] + " ");
  10. }
  11. System.out.println();
  12. }
  13. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Ordering an array

 
0
  #3
Oct 25th, 2007
Instead of looping over the entire array to display it's contents, a clever way would be to convert it to a list and display it using it's toString() method (called implicitly).

System.out.println(Arrays.asList(myArray));
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: Zonr_0 is an unknown quantity at this point 
Solved Threads: 3
Zonr_0 Zonr_0 is offline Offline
Newbie Poster

Re: Ordering an array

 
0
  #4
Oct 26th, 2007
Thank you very much, I was hoping there was something the API I was missing!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Ordering an array

 
0
  #5
Oct 26th, 2007
And remember that Python lists are NOT arrays.
Lists in Java also can be easily sorted (if there's an ordering defined for the content of course).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC