Hey guys i'm new to java and this is the first time i'm posting a question on daniweb so please bear with me ...

I have made a quiz for a school project and its almost done save a high scoring mechanism.
What i want is first ask the user whether he/she would like to save the score and if so i call another function with the score (int) & the user's name (String) as parameters.
Now I want this function to save the scores and the respective username in a file in descending order and only the top 10 scores are to be saved thus i also want to see whether the user's score is good enough ...
i wrote a function using arrays, files and strings to do this but the problem is that only one name gets written and the rest remain blank ... and i've tried both the 'true' form of the file declaration as well as the one without it ... here's my code ...

public class Score()
{public static void score(int score, String name)
Score s=new Score();
FileWriter fw=new FileWriter("Scores.txt", true); //same thing happens even without 'true'
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
FileReader fr=new FileReader("Scores.txt");
BufferedReader br=new BufferedReader(fr);
String ns[]=new String [11];
int i, j;
for(i=0;i<11;i++) //initializing all elements to null
String newScore=s.codeDecode(score); //making sure that the scores have equal no of
//digits so that i can use compareTo()
ns[10]=newScore + "\t\t\t" + name;
String text="", temp="";
ns=text.toString(); //reading the contents of the file and storing each line in
i++;                        //a diff. element so that each contains a username & score
{if((ns.compareTo(ns[j])<0)) //if score in ns is lower than that in ns[j]
{temp=ns.toString();         //i swap the 2 elements
ns=ns[j].toString();          //thus the array comes in descending order
{pw.println(ns); //i print the elements of the array on to the file

public static String codeDecode(int score)
{int i=0, d, copy=score;
{copy/=10; //counting no of digits in score
int z=3-i; //Since maximum score possible is 999
String newScore="";
{newScore="0" + newScore; //appending equivalent no of zeros
newScore=newScore + score;
return newScore;

I dont see why this function isnt working but to give you a better idea of the problem here's an example

Say, A scores 100, then B scores 75, then C scores 120
Ideally, the file should get written thus

C         120
A         100
B         75

However what actually happens is

C     120

thats it
and its not because c is the maximum value
Whatever values i enter at the end only that value gets stored .
So please if there's anyone who could help me out.
This project's due in a week and my exams start in a week as well so i really need to finish it fast.

I have a few suggestions for you:

1) It would be advisable to use the .length attribute of the array object. So instead of for(i=0;i<10;i++) please uses for (i=0; i < ns.length; i++) Btw, why do you suppose we have access to the .length attribute of the array object? 'Cause it's declared final :icon_wink:

2) Another useful class to use when dealing with a text file is the Scanner class from the java.util package. With this class you can actually read-in the numbers as an int (which you can do anyway, but not this easily). But of course you don't need that, but I do feel in makes reading text from a file real simple :icon_idea:

3) Let's take a look at you're selection sort, it's a bit flawed (but no enough so for your program to not work). You should have:

for (i = 0; i < ns.length - 1; i++)
  for (j = i + 1; j < ns.length; j++)

Of course in this case ns.length has a value of 11 so you'd need 10 in the outer-loop instead of 11. Why do we not access the last element in the outer-loop? Because the last element would have found its correct place when the second-last element was sorted. You should check out Wikipedia for more information :icon_cool:

4) You should not be creating a new instance of your class (i.e. don't use Score s=new Score(); ) just to gain access to the codeDecode method. You'd simply call the static method from the class: String newScore = Score.codeDecode(score); This is the purpose of creating a static method :icon_smile:

I'm actually not really sure why you're program isn't working hey :icon_confused: I can't really spend too much time looking at your code but I think you should look at the contents of the array after you're read the data from the file and the contents of the array after you sort - by simply displaying it:

for(String element: ns)


for (i = 0; i < ns.length; i++)

both have the same effect :icon_biggrin: Hope you find the bug!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.