473,396 Members | 1,879 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,396 software developers and data experts.

sorting problem

35
Im trying to sort a set of names, stored in a textfile in acscending order (this textfile contains also the marks of each student). I did this piece of code, but altough its compiling its not sorting them. Can someone pls help me with this problem. Thanks a lot.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.io.DataInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5.  
  6. import javax.swing.JDialog;
  7. import javax.swing.JTextArea;
  8. import java.awt.*;
  9. import javax.swing.*;
  10.  
  11. import java.io.*;
  12. import java.util.Arrays;
  13.  
  14.  
  15.  
  16. public class Scores {
  17. int len;
  18. private JTextArea textArea;
  19. public Scores() {
  20. super();
  21.  
  22. try{
  23. JDialog dialog = new JDialog();
  24. dialog.getContentPane().setLayout(new BorderLayout(10, 10));
  25. //
  26. String record = null;
  27. String path = "players.txt";
  28.  
  29.         File file = new File(path);
  30.         StringBuilder sb = new StringBuilder();
  31.         BufferedReader br = new BufferedReader(
  32.                             new InputStreamReader(
  33.                             new FileInputStream(file)));
  34.         String line;
  35.         while((line = br.readLine()) != null) {
  36.             sb.append(line + "\n");
  37.         }
  38.         br.close();
  39.         String[] p = sb.toString().split("\\n");
  40.         System.out.printf("items = %s%n", Arrays.toString(p));
  41.  
  42.  
  43. int a,b;
  44.  
  45. String temp;
  46.  
  47. int sortTheStrings = len - 1;
  48.  
  49. for (a = 0; a < sortTheStrings; ++a)
  50.  
  51. for (b = 0; b < sortTheStrings; ++b)
  52.  
  53. if(p[b].compareTo(p[b + 1]) >0)
  54.  
  55. {
  56.  
  57. temp = p[b];
  58.  
  59. p[b] = p[b + 1];
  60.  
  61. p[b + 1] = temp;
  62.  
  63. }
  64.  
  65.  
  66. FileInputStream fileInput = new FileInputStream(file);
  67. DataInputStream dis = new DataInputStream(fileInput);
  68.  
  69. String string = dis.readLine();
  70. textArea = new JTextArea();
  71. while(string != null){
  72. textArea.append(string + "\n");
  73. string = dis.readLine();
  74. textArea.setBackground(Color.orange);
  75. textArea.setFont(new Font("Times New Roman", Font.BOLD, 14));
  76. textArea.setEditable(false);
  77. textArea.setForeground(Color.red);
  78. }
  79. dialog.getContentPane().add(BorderLayout.CENTER, textArea);
  80. dialog.setTitle("Scores");
  81. dialog.setSize(350, 350);
  82.  
  83. dialog.setVisible(true);
  84.  
  85.  
  86. }catch (Exception e){
  87. e.printStackTrace();
  88. }
  89.  
  90. }
  91.  
  92. public static void main(String[] args){
  93. new Scores();
  94. }
  95.  
  96. }
Feb 17 '08 #1
0 978

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
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)) ...
4
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
7
Plater
by: Plater | last post by:
I am having trouble determining when my DataGridView object is sorting (based on a column header click). The idea is, in a large table, sorting the columns takes time, so I show a splash screen....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.