Help with J2ME RS

Hi all, new here and only been doing java for about a month. I am writing a blackjack game and it’s almost done but I want to use a recordstore for the stats and am having a problem… Here is my code to read/write the RS

private boolean readRS() {
    RecordStore rs;
    try {
        rs = RecordStore.openRecordStore("BlackJack", true);
        if (rs.getNumRecords() == 1) {
            System.out.println("loading settings");
	ByteArrayInputStream buffer = new ByteArrayInputStream(rs.getRecord(1));
	DataInputStream stream = new DataInputStream(buffer);
			
	long sv = stream.readLong();
	if (sv != this.SETTINGSVERSION) { //previous settings format is diferrent
                rs.closeRecordStore(); 
                wins = 0;
                losses = 0;
                pushes = 0;
                blackJacks = 0;
                bankRoll = 100000;
                soundOn = true;
                return writeRS(); //create new format;
                 //othewise it may cause a crash if we read different format
	}
			
	wins = stream.readInt();
            losses = stream.readInt();
            pushes = stream.readInt();
            blackJacks = stream.readInt();
            bankRoll = stream.readLong();
            soundOn = stream.readBoolean();
            rs.closeRecordStore();	
            System.out.println("Settings read...");
            return true;
        } else {
            wins = 0;
            losses = 0;
            pushes = 0;
            blackJacks = 0;
            bankRoll = 100000;
            soundOn = true;
            rs.closeRecordStore();					
            return writeRS();
        }   
    
    } catch (Exception e) {
    
        db(e.toString());
    }
    return false;
}

public boolean writeRS() {
    RecordStore rs;
try {
        rs = RecordStore.openRecordStore("BlackJack", true);
        ByteArrayOutputStream buffer=new ByteArrayOutputStream();
        DataOutputStream stream=new DataOutputStream(buffer);
		
        stream.writeLong(SETTINGSVERSION);
        stream.writeInt(wins);
        stream.writeInt(losses);
        stream.writeInt(pushes);
        stream.writeInt(blackJacks);
        stream.writeLong(bankRoll);
        stream.writeBoolean(soundOn);
        
        //if (rs.getNumRecords() == 1)
	rs.setRecord(1, buffer.toByteArray(), 0, buffer.size());
    //else
    //	rs.addRecord(buffer.toByteArray(), 0, buffer.size());
		
        rs.closeRecordStore();					
    
        System.out.println("Settings written...");
        
    } catch (Exception e) {
    
        db(e.toString());
        return false;
    }
    return true;
		      
}

I am sure there is some basic thing I am missing but, it has been driving me nuts for 2 days :frowning:

Thanks for any help you can give me

Kevin

I forgot to say what the actual problem is… When the program loads, it should read the stats (if they exist) and default to 0’s if not. But, for some reason, the stats always start at 0 if they exist or not.

Kevin

It’s a while since I did any J2ME but I don’t remember any input/outputstreams… try setRecord() & getRecord() & save your data as bytes. I think some phones don’t like recordIDs which != 1… or was it 0? ???

Just checked - it’s 1.

Here’s working code;

	/***************************************************************************
	 **************************************************************************/
	public void saveGameState()
	{
		byte[] data = new byte[1024];
		
		// insert save data into byte array here, ie data[0]=(byte)(anIntVariable&255);data[1]=(byte)((anIntVariable>>8)&255); &c
		
		try
		{
			RecordStore rs = RecordStore.openRecordStore("myRMS", true);
			try {
				rs.setRecord(1, data, 0, data.length);
			} catch (RecordStoreException ex) {
				rs.addRecord(data, 0, data.length);
			}
			rs.closeRecordStore();
		}
		catch (RecordStoreException ex)	{
		}
	}
	/***************************************************************************
	 **************************************************************************/
	public void loadGameState()
	{
		try
		{
			RecordStore rs = RecordStore.openRecordStore("myRMS", true);
			byte[] data = rs.getRecord(1);
			
			// get data from byte array here
			
			rs.closeRecordStore();

		}
		catch (RecordStoreException ex)	{
			
			//set default values here;
			
		}
	}

I’ll give it a shot. Thanks a million for the help.

Kevin