2

When I parse an empty string from a textview, it says unknown int "" when I already set a conditional if that takes that case. Code as below.

else if (naqaBoolean) {
                    int i;
                    int durasiHaidh = 24;
                    int tempohSebenar = 0;
                    for (i = 1; i < 17; i++){
                        TextView tvStatus = findViewById(getResources().getIdentifier("status" + i, "id", MainActivity.this.getPackageName()));
                        TextView tvTempoh = findViewById(getResources().getIdentifier("tempoh" + i, "id", MainActivity.this.getPackageName()));
                        TextView tvMenstruasi = findViewById(getResources().getIdentifier("menstruasi" + i, "id", MainActivity.this.getPackageName()));
                        int hariIni = 0;
                        String text = (String.valueOf(tvTempoh.getText()));
                        if(text == "Seharian"){
                            hariIni = 24;
                        } else if (text == "-"){
                            hariIni = 0;
                        } else if (text == ""){
                            hariIni = 0;
                        } else {
                            hariIni = Integer.parseInt((String) tvTempoh.getText());
                        }

                            tempohSebenar = tempohSebenar + hariIni;
                    }

                    Toast.makeText(getApplicationContext(), tempohSebenar, Toast.LENGTH_SHORT).show();
                }

1 Answers1

0

At the beginning you use:

String text = (String.valueOf(tvTempoh.getText()));

and test that with your ifs.

Then, in the else block, you parse (String) tvTempoh.getText()); to an int. Instead of that, you should use Integet.parseInt(text);

Also, String comparision should not be done with ==, but with .equals because == is a reference comparision. It works sometimes if the Strings are cached but you cannot be sure.

It's because String.valueOf() is not the same as a cast ((String)).

dan1st
  • 12,568
  • 8
  • 34
  • 67