1 Replies - 933 Views - Last Post: 20 July 2011 - 01:43 AM

#1 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

error writing record java rms after deleting

Posted 18 July 2011 - 02:51 PM

I have been having a bit of trouble with RMS deleting records, and then writing records to the database after a record has been deleted.

Basically I am trying to set up a list of recent users, so as not to type out a name time after time. In the newUsr method, I add a user entered name to the database, unless the name already exists in the database. If the database already has 5 recent users names, I selct a name by random. It would be much better if I could select it by the date of the name that was hasnt been used the longest.

I have thought about reading all elements into a vector of strings, working with the vector. removing all elements in the database, and then once done adding all elements from the vector to the database.

any comments on cleaning my code up and critisms will be welcome too if guid me on how to better my programming style.


 public void writeToRecentUsersDB(String ruName){
        byte[] bstringArray = ruName.getBytes();
        try {
            
            dbRecentUsers.addRecord(bstringArray, 0, bstringArray.length);
           
        } catch (RecordStoreException ex) {
            
        }
    }

public void newUser(){
        boolean playON = true;
        int xFontPos = 50;
        long lCurrTick = 0; // current system time in milliseconds;
        int iKey = 0;
        boolean option3isRunning = true;
        Random rand = new Random();
        boolean toAdd = true;
        //int rand5 = rand.nextInt(5);
        
        while(option3isRunning){
            
            lCurrTick = System.currentTimeMillis();
            
            iKey = getKeyStates();
            checkKeys(iKey, lCurrTick);
            updatePrompt(lCurrTick);
            
            //if(isDown[fireKey]){
            if(keyPressedCode == TOP_LEFT_KEY){//acknowledge
                name = prText;
                
                String str;
                int rndIndex = 0;
                cleanScreen();
                openRecentUsersDB(RECENT_USERS);
                try {
                    
                    int testrms = dbRecentUsers.getNumRecords();
                    if( testrms > 0){
                        for(int index =1; index < testrms+1 ; index++){//added one as rms databases work from 1 to ...
                            str = new String(dbRecentUsers.getRecord(index));
                            if (str.equals(name)){
                                rndIndex = index;
                                toAdd = false; // or rather not to write to record
                            }//if
                        }//for
                    }//if
                    
                    //dbRecentUsers.deleteRecord(rndIndex);
                    
                    // insert name into recent names database - max of 5
                    testrms = dbRecentUsers.getNumRecords();
                    int testrms2 = dbRecentUsers.getSize();
                    if(testrms == 5){
                        try {//delte a random record.
                            //if name is not being used
                            rndIndex = rand.nextInt(5)+ 1;
                            
                            if(rndIndex == 6)
                                rndIndex = 1;
                            
                            dbRecentUsers.deleteRecord(rndIndex); // for now. actaully want a date recent played.
                            
                        } catch (RecordStoreException ex) {
                        }
                        
                    }
                    testrms = dbRecentUsers.getNumRecords();
                    if(testrms < 5){
                        
                        if(toAdd){
                            writeToRecentUsersDB(name);
                        }
                    }
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }
                closeRecentUsersDB();
                option3isRunning = false;
                
                
            }



Is This A Good Question/Topic? 0
  • +

Replies To: error writing record java rms after deleting

#2 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

Re: error writing record java rms after deleting

Posted 20 July 2011 - 01:43 AM

Ok, So I have come to the conclusion that deleting from a record store isn't a good idea, and so I have used a vector. I read all elements into the vector from the record store, find the element i want to delete, delete it from the vector, delete the record store, creat a new record store with the same name, add all the elements from the vector into the record stor, and then adding should go alright.

The emulator so far just duplicates what was in the old record store, SO I did a bit of research, and it appears that closing a record store, doesnt actualy close it if the record store has benn opened more times than it has been close. There needs to be a balance of closed record stores and opened ones.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1