Ok thanks a lot. But the problem is that in this texfile i have stored not only the scores but also the names since this is like a quiz and it is displaying the high score.
This is the piece of code that displaysthe high scores together with the names. Can you pls help me, in making the scores being sorted with the highest mark displayed on top together with the names.
Thanks a lot. :-)
This is the code that displays the scores and names from textfile:
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 {
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("players.txt");
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();
}
}
And this code is used to write the names together with the score to the textfile:
String name = JOptionPane.showInputDialog(null, "Enter name");
String type = JOptionPane.showInputDialog(null, "Enter your quiz type");
out = new BufferedWriter(new FileWriter("players.txt",true));
out.write(name); //Writing to the textfile (the name entered by user)
String trimmedStr = name.trim();
out.write(String.valueOf(percent+ " % "));
out.write(" ");
out.newLine(); //write a new line to the textfile
out.close();
I tried doing somethig like this, but it isn't working.
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();
}
}
Thanks a lot again. :-)