| | |
Help needed for saving high scores
![]() |
•
•
Join Date: Jan 2008
Posts: 1
Reputation:
Solved Threads: 0
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
{ns[i]="";
}
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="";
i=0;
while((text=br.readLine())!=null)
{System.out.println(ns[i]);
ns[i]=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(i>9)
break;
}
for(i=0;i<10;i++)
{for(j=i+1;j<11;j++)
{if((ns[i].compareTo(ns[j])<0)) //if score in ns[i] is lower than that in ns[j]
{temp=ns[i].toString(); //i swap the 2 elements
ns[i]=ns[j].toString(); //thus the array comes in descending order
ns[j]=temp;
}
}
}
for(i=0;i<10;i++)
{pw.println(ns[i]); //i print the elements of the array on to the file
}
pw.close();
}
public static String codeDecode(int score)
{int i=0, d, copy=score;
while(copy!=0)
{copy/=10; //counting no of digits in score
i++;
}
int z=3-i; //Since maximum score possible is 999
String newScore="";
for(i=1;i<=z;i++)
{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.
Thanks.
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
{ns[i]="";
}
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="";
i=0;
while((text=br.readLine())!=null)
{System.out.println(ns[i]);
ns[i]=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(i>9)
break;
}
for(i=0;i<10;i++)
{for(j=i+1;j<11;j++)
{if((ns[i].compareTo(ns[j])<0)) //if score in ns[i] is lower than that in ns[j]
{temp=ns[i].toString(); //i swap the 2 elements
ns[i]=ns[j].toString(); //thus the array comes in descending order
ns[j]=temp;
}
}
}
for(i=0;i<10;i++)
{pw.println(ns[i]); //i print the elements of the array on to the file
}
pw.close();
}
public static String codeDecode(int score)
{int i=0, d, copy=score;
while(copy!=0)
{copy/=10; //counting no of digits in score
i++;
}
int z=3-i; //Since maximum score possible is 999
String newScore="";
for(i=1;i<=z;i++)
{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.
Thanks.
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
I have a few suggestions for you:
1) It would be advisable to use the .length attribute of the array object. So instead of
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
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:
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
4) You should not be creating a new instance of your class (i.e. don't use
I'm actually not really sure why you're program isn't working hey
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:
or
both have the same effect
Hope you find the bug!
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 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
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:
java Syntax (Toggle Plain Text)
for (i = 0; i < ns.length - 1; i++) for (j = i + 1; j < ns.length; j++)
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 I'm actually not really sure why you're program isn't working hey
java Syntax (Toggle Plain Text)
for(String element: ns) System.out.println(element);
java Syntax (Toggle Plain Text)
for (i = 0; i < ns.length; i++) System.out.println(ns[i]);
Last edited by PoovenM; Jan 9th, 2008 at 4:03 am.
![]() |
Other Threads in the Java Forum
- Previous Thread: adding a JFrame component to JTabbedPane
- Next Thread: add a query add a list
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows





