【问题标题】:Can anyone help with a Java problem?任何人都可以帮助解决 Java 问题吗?
【发布时间】:2009-03-20 14:28:23
【问题描述】:

我有这个代码,但由于某种原因我无法让它工作?

谁能看出问题所在?

我需要此数据才能返回包含在 mNameSearchDelete 文本框中输入的相同名称的所有记录。但是没有返回应该存在的记录。

谢谢

protected void searchForNameToDelete() {
    carryOutNameSearch(mNameSearchDelete, mListFormDelete);
}
private void carryOutNameSearch(TextBox theInputScreen, Form theOutputForm) {
   listOfIDs = new Vector();  // save IDs of records in case we want to delete
   theOutputForm.deleteAll(); // clear the form
    try {
        RecordStore rs = RecordStore.openRecordStore("EventsDatabase", true);

        // Use the inner class so that the enumeration only gives
        // us those records with a matching name.
        RecordEnumeration re = rs.enumerateRecords(new NameMatcher(theInputScreen.getString()), null, false);
        while (re.hasNextElement()) {
            int id = re.nextRecordId();
            listOfIDs.addElement(new Integer(id));
            byte [] recordBuffer = rs.getRecord(id);
            String record = new String(recordBuffer);

            // extract the name and the age from the record

            int endOfnameEvent = record.indexOf(";");
            int endOfdescEvent = record.indexOf(";", endOfnameEvent + 1);

            String name = record.substring(0, endOfnameEvent);
            String desc = record.substring(endOfnameEvent + 1, endOfdescEvent);
            theOutputForm.append(name + " description: " + desc + "\n");
        }
        rs.closeRecordStore();
    }
    catch(Exception e){
       // mAlertConfirmDetailsSaved.setString("Couldn't read details");
        System.err.println("Error accessing database");
    }
    Display.setCurrent(theOutputForm);

}

  /* An inner class to allow us to select only
*  those records with a matching name.
*/
static class NameMatcher  implements RecordFilter {
    String nameToMatch;
    public NameMatcher(String nameEvent) {
        nameToMatch = nameEvent;
    }
    public boolean matches(byte[] record) {
        if (record.length < nameToMatch.length()) {
            return false;
        }
        String strRecord = new String(record);
        if (strRecord.startsWith(nameToMatch)) {
            return true;
        }
        return false;
    }
}

【问题讨论】:

  • 也许您应该考虑描述一下究竟是什么不起作用。我们不介意读者。
  • 告诉我们它是做什么的,然后告诉我们它做了什么是错的。如果你连这件事都懒得做,那你就不值得帮助,IMNSHO。
  • 我同意,请输入您的代码不适用的内容。
  • 完全同意(并且反对)
  • 对不起,我需要它来显示数据库中与输入的数据相同的记录。当我知道记录在那里时,它会显示一个空白表格。如果你能帮助我会appeciate

标签: java java-me


【解决方案1】:

我可以看出一件事是错误的:

if (record.length < nameToMatch.length()) {
    return false;
}

您正在将多个字节与多个字符进行比较。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2018-06-03
    相关资源
    最近更新 更多