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.
Code:
import java.awt.BorderLayout; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import javax.swing.JDialog; import javax.swing.JTextArea; import java.awt.*; import javax.swing.*; import java.io.*; import java.util.Arrays; public class Scores { int len; private JTextArea textArea; public Scores() { super(); try{ JDialog dialog = new JDialog(); dialog.getContentPane().setLayout(new BorderLayout(10, 10)); // String record = null; String path = "players.txt"; File file = new File(path); StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); String line; while((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); String[] p = sb.toString().split("\\n"); System.out.printf("items = %s%n", Arrays.toString(p)); int a,b; String temp; int sortTheStrings = len - 1; for (a = 0; a < sortTheStrings; ++a) for (b = 0; b < sortTheStrings; ++b) if(p[b].compareTo(p[b + 1]) >0) { temp = p[b]; p[b] = p[b + 1]; p[b + 1] = temp; } FileInputStream fileInput = new FileInputStream(file); DataInputStream dis = new DataInputStream(fileInput); String string = dis.readLine(); textArea = new JTextArea(); while(string != null){ textArea.append(string + "\n"); string = dis.readLine(); textArea.setBackground(Color.orange); textArea.setFont(new Font("Times New Roman", Font.BOLD, 14)); textArea.setEditable(false); textArea.setForeground(Color.red); } dialog.getContentPane().add(BorderLayout.CENTER, textArea); dialog.setTitle("Scores"); dialog.setSize(350, 350); dialog.setVisible(true); }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args){ new Scores(); } }