okay so i’ve got some code and its kind of working but i’m having trouble…
I want to retrieve my enumerated scores from the database and print them to a string…
but this only seems to be setting the latest score. It does work in the output bar as println but not when i put it to string.
package game;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class HighScore extends MIDlet implements CommandListener
{
TextField name, score;
Command add, show, exit, home;
private String HS;
private Form f;
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException
{
f = new Form("High scores");
f.append(name = new TextField("Name", "", 255, TextField.ANY));
f.append(score = new TextField("Score", "", 10, TextField.NUMERIC));
f.addCommand(add = new Command("Add new score", Command.OK, 1));
f.addCommand(show = new Command("Retrieve scores", Command.OK, 2));
f.addCommand(exit = new Command("Exit", Command.EXIT, 1));
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}
public void commandAction(Command c, Displayable d)
{
if(c == home)
{
Display.getDisplay(this).setCurrent(f);
}
if(c == exit)
{
notifyDestroyed();
}
else if(c == add)
{
try
{
addNewScore(name.getString(), score.getString());
showConfirm("Score correctly added");
}
catch(Exception e)
{
showError(e.toString());
}
}
else if(c == show)
{
try
{
String[][] scores = retrieveHighScores();
System.out.println("HIGH SCORES");
for(int i = 0; i < scores.length; i++)
{
System.out.println("SCORE: " + scores[i][0] + " - " + scores[i][1]);
HS = ("SCORE: " + scores[i][0] + " - " + scores[i][1]);
}
showConfirm("Scores correctly retrieved");
}
catch(Exception e)
{
showError(e.toString());
}
}
}
void showConfirm(String text)
{
Display.getDisplay(this).setCurrent(new Alert("Score Added", text, null, AlertType.CONFIRMATION));
Form highscore = new Form("Your Score");
highscore.append(HS);
highscore.addCommand(home = new Command("Home", Command.OK, 1));
highscore.addCommand(exit = new Command("Exit", Command.EXIT, 1));
highscore.setCommandListener(this);
Display.getDisplay(this).setCurrent(highscore);
}
void showError(String error)
{
Display.getDisplay(this).setCurrent(new Alert("Error", error, null, AlertType.ERROR));
}
void addNewScore(String name, String score) throws Exception
{
// We open the recordstore
RecordStore highscore = RecordStore.openRecordStore("High Score", true);
// To add a new HiScore we use a quick string comma-separated
String str = new String(name + "," + score);
byte [] strb = str.getBytes();
highscore.addRecord(strb, 0, strb.length);
highscore.closeRecordStore();
}
String[][] retrieveHighScores() throws Exception
{
String[][] scores;
RecordStore highscore = RecordStore.openRecordStore("High Score", true);
// To read all hiscores saved on the RMS
RecordEnumeration enumerator = highscore.enumerateRecords(null, new ScoresComparator(), false);
int id, index, separator;
byte[] record;
String str;
index = 0;
scores = new String[enumerator.numRecords()][2];
while (enumerator.hasNextElement( )) {
id = enumerator.nextRecordId( );
record = highscore.getRecord(id);
str = new String(record);
// Operate with str to extract name and points
separator = str.indexOf(',');
scores[index][0] = str.substring(0, separator);
scores[index][1] = str.substring(separator + 1);
index++;
}
highscore.closeRecordStore();
return scores;
}
class ScoresComparator implements RecordComparator
{
public int compare(byte[] arg0, byte[] arg1)
{
int score0 = 0, score1 = 0;
for(int i = 0; i < arg0.length; i++)
{
if(arg0[i] == ',')
{
score0 = Integer.parseInt(new String(arg0, i + 1, arg0.length - i - 1));
}
}
for(int i = 0; i < arg1.length; i++)
{
if(arg1[i] == ',')
{
score1 = Integer.parseInt(new String(arg1, i + 1, arg1.length - i - 1));
}
}
if(score0 < score1)
return 1;
else if(score0 == score1)
return 0;
else
return -1;
}
}
}
Any Help Appreciated Cheers