hmm, this is a strange one.
ive got a bit of code that is supposed to search a database of records, comparing the records to a
string, and if it matches, it should return the matched record as a string. the problem is, it will only work with the last record in the database, otherwise i get a stringindexoutofbounds exception. the search code is below:
private synchronized Object action(String s, byte[] data, int action)
throws RecordStoreNotOpenException, RecordStoreFullException,
RecordStoreException {
if ((action != 1) && (recordIDs.size() == 0)) {
throw new RecordStoreException();
}
int IDs = database.getNumRecords();
System.err.println("no of records in the database is " +IDs);
for(int index=1;index <IDs+1;index++) {
try {
System.err.println("index is "+ index);
System.err.println(s);
System.err.println("the record at position "+index+" is " + new String(database.getRecord(index)));
if (rc.compare(database.getRecord(index),s.getBytes())== RecordComparator.EQUIVALENT) {
switch (action) {
case 0:
database.deleteRecord(index);
recordIDs.removeElement(new Integer(index));
return null;
case 1:
return new String(database.getRecord(index));
case 2:
database.setRecord(index, data, 0, data.length);
return null;
default:
break;
}
}
} catch (InvalidRecordIDException iri) {
throw new RecordStoreException(iri.getMessage());
}
}//for loop ends here
return null;
}
the exception error i get and all the associated gubbins is also below:
During event handling, midlet threw java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(+45)
at example.stock.Stock.parse(+24)
at example.stock.Stock.getName(+14)
at example.stock.StockDatabase$StockComparator.compare(+23)
at example.stock.Database.action(+169)
at example.stock.Database.search(+7)
at example.stock.StockMIDlet.displayProduct(+8)
at example.stock.StockMIDlet.access$600(+5)
at example.stock.StockMIDlet$StockCommandListener.commandAction(+191)
at javax.microedition.lcdui.List.keyPressed(+44)
at javax.microedition.lcdui.Display$DisplayAccessor.keyEvent(+68)
at com.sun.midp.lcdui.DefaultEventHandler$2.run(+193)
i did alter this search as it originally searched using a vector of record IDs as the index, but im not sure if the way i have adapted it (ie using a for loop) is kosher. if anyone spots any glaringly obvious mistakes, please let me know, so i can bang my head off the desk.
thanks in advance
dave