java - 我的 try catch 功能无法按预期工作
问题描述
我的程序应该采用给定的值并用它找到另一个值。但是,我的 try catch 语句似乎不起作用。它从不执行前 2 个 if 语句,这是程序最重要的部分。当您输入两个值时,第三个确实有效。提前致谢。
public void calculate(View view) {
EditText length_find = findViewById(R.id.feet);
EditText pounds_find = findViewById(R.id.pounds);
try {
//int length_int = Integer.parseInt(length_find.getText().toString());
//int pounds_int = Integer.parseInt(pounds_find.getText().toString());
double d = .29;
//double length = length_int;
//double pounds = pounds_int;
double w = 24.5 / 12;
double h = .002 / 12;
//This Calculates if Pounds are given
if ((length_find).getText().toString().trim().length() == 0){
Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
int pounds_int = Integer.parseInt(pounds_find.getText().toString());
double pounds = pounds_int;
double v = pounds / d;
double length = v / w / h;
final TextView mTextView = (TextView) findViewById(R.id.length_show);
mTextView.setText((int) length);
}
//This Calculates if Length is given
if ((pounds_find).getText().toString().trim().length() == 0){
Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
int lenght_int = Integer.parseInt(length_find.getText().toString());
double length = lenght_int;
double v = length * w * h;
double answer_pounds = v * d;
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText((int) answer_pounds);
}
if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
}
}
catch(Exception ex) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
解决方案
据我了解,
(pounds_find).getText().toString().trim().length() <= 0
或者
(length_find).getText().toString().trim().length() <= 0
仅当用户没有输入任何内容时才为真。如果他有,那么第三个“如果”按预期执行。但是当用户没有输入任何内容时
int length_int = Integer.parseInt(length_find.getText().toString());
int pounds_int = Integer.parseInt(pounds_find.getText().toString());
无法完成。因为它是空的。我认为这里就是这种情况。如果不是,请告诉我。如果我错了对不起。这就是我从这里理解的。如果您可以调试并具体说明场景或异常(如果它抛出任何异常),那将更有帮助
推荐阅读
- ios - 如何使用一个视图控制器切换/更新视图控制器?
- mysql - 通过链接表的 MySQL m:n 关系查询在生产 8.0.13 中返回 null 但在本地工作 5.7.30
- python - 无法使用定义的变量python
- javascript - 私有属性的行为类似于打字稿中的公共属性
- python - 从同一个脚本 Python 运行 2 个 web.py 服务器
- javascript - 无法从Angular 7本地文件夹中的JSON文件中读取数据
- flutter - 如何在颤动的抽屉内再添加一个抽屉?
- r - 将多个数据框粘贴在一起,同时将数据保留在特定的行和列中
- python - 跨多个函数调用的 Python 3.8 递归函数保存列表?
- python - 在 Python3 中打印列表