gpt4 book ai didi

java - if + try - catch 方法无法正常工作

转载 作者:行者123 更新时间:2023-12-02 07:08:03 25 4
gpt4 key购买 nike

我正在开发一个基于 SQLite 的应用程序。除了我的方法中的 if-else 语句之外,一切都工作正常。储蓄之类的东西有效,只是检查就让我血压相当高。我希望你们中的一个人比我聪明得多,并发现我犯的可能是明显的错误:

public void save() {

// get length of EditText
int dateLength, mileageLength, amountLength, lpriceLength, tpriceLength;

dateLength = date_widget.getText().length();
mileageLength = mileage_widget.getText().length();
amountLength = amount_widget.getText().length();
lpriceLength = price_widget.getText().length();
tpriceLength = totalPrice_widget.getText().length();

// Start save method if EditTexts are not empty.

if (dateLength > 0 || mileageLength > 0 || amountLength > 0
|| lpriceLength > 0 || tpriceLength > 0) {

// Get the value of each EditText and write it into the
// String/doubles

String date = date_widget.getText().toString();
double mileage = Double
.valueOf(mileage_widget.getText().toString());
double amount = Double.valueOf(amount_widget.getText().toString());
double lprice = Double.valueOf(price_widget.getText().toString());
double tprice = Double.valueOf(totalPrice_widget.getText()
.toString());

// Check if mileage is increasing, else cancel and show toast
int checkMileage = Integer.parseInt(db
.getSearchResult("mileage", 0));

if (checkMileage < mileage) {

try {
// if (id == null) {
db.insert(date, mileage, amount, lprice, tprice);

Toast.makeText(this, R.string.action_input_saved,
Toast.LENGTH_SHORT).show();
finish();

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "ERROR " + e, Toast.LENGTH_LONG)
.show();
}

} else {
Toast.makeText(
this,
"Your current mileage must be more than the last saved mileage",
Toast.LENGTH_LONG).show();
}

} else {
Toast.makeText(this, "finish your input", Toast.LENGTH_LONG).show();
}

}

DbAdapter 类中的我的方法:

public String getSearchResult(String sql, int cmd) {

if (cmd == 0) {
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
cursor.moveToFirst();

String tmp = cursor.getString(0);
cursor.close();

// return count
return tmp;
} else if (cmd == 1) {
int sum = 0;
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
String idQuery = "SELECT _id FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
Cursor id = db.rawQuery(idQuery, null);
// berechnung
cursor.moveToFirst();
id.moveToFirst();

int maxId = Integer.parseInt(id.getString(0));
for (int i = 0; i < maxId; i++) {

int tmp = Integer.parseInt(cursor.getString(0));
sum = sum + tmp;
cursor.moveToNext();
}
cursor.close();
id.close();
return String.valueOf(sum);
} else if (cmd == 2 && sql == "mileage") {
int sum = 0;
String countQuery = "SELECT " + sql + " FROM " + TABLE_NAME;
String idQuery = "SELECT _id FROM " + TABLE_NAME
+ " WHERE _id = (SELECT max(_id) FROM " + TABLE_NAME + ")";
Cursor cursor = db.rawQuery(countQuery, null);
Cursor id = db.rawQuery(idQuery, null);
// berechnung
cursor.moveToFirst();
id.moveToFirst();

int maxId = Integer.parseInt(id.getString(0));
if (maxId > 1) {
int array[] = new int[maxId];

// Array füllen
for (int i = 0; i < maxId; i++) {

array[i] = Integer.parseInt(cursor.getString(0));
// sum = sum + tmp;
cursor.moveToNext();
}
for (int k = 1; k < maxId; k++) {
int tmp;
tmp = array[k] - array[k - 1];
sum = sum + tmp;
}

cursor.close();
id.close();
return String.valueOf(sum);
} else {
return "--";
}

}
return "Wrong CMD";

}

我知道我很乱

最佳答案

将评论转化为答案:

在第一个 if 中将所有 || 切换为 &&。否则,即使只填写了一个字段,您也会尝试处理所有内容。

关于java - if + try - catch 方法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15862315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com