user3562918
user3562918

Reputation: 76

Java Error substring

I have code like this :

List<RFeeAtmbVO> list = RFeeAtmbBO.getInstance().listRFeeAtmb(mapParam, null);
if (list != null && list.size() > 0) {
  for (RFeeAtmbVO vo : list) {
    vo.setIdFeeType(edit_biaya_jenisBiayaId.getValue());
    String name = edit_biaya_name.getValue().trim();
    if (vo.getName().indexOf("(Gagal Berbayar)") > -1) {
      name = name.substring(0, name.indexOf(" (Gagal Berbayar)")) + " (Gagal Berbayar)";
      System.out.println("Hasilnya 1: "+name);
    } else {
      if (name.indexOf("(Gagal Berbayar)") > -1 ) {
        name = name.substring(0, name.indexOf(" (Gagal Berbayar)"));
        System.out.println("Hasilnya2 : "+name);
      }
    }
    vo.setName(name);
    vo.setSdate(edit_biaya_sdate.getValue());
    vo.setEdate(edit_biaya_edate.getValue());
    vo.setKodeFee(edit_biaya_kodeFee.getValue());
    vo.setMappingCode(edit_biaya_mappingCode.getValue());
    RFeeAtmbBO.getInstance().updateRFeeAtmb(vo);
  }
}

When I add in textbox with "(Gagal Berbayar)" without quote is no problem and saved in database, but add some text without "(Gagal Berbayar)", I get the error like this :

[err] java.lang.StringIndexOutOfBoundsException: String index out of range: -1
[err]   at java.lang.String.substring(String.java:1904)
[err]   at controller.fee.fee.EditBiayaController.onClick$edit_biaya_btnSave(EditBiayaController.java:413)

And Line 413 is

name = name.substring(0, name.indexOf(" (Gagal Berbayar)")) + " (Gagal Berbayar)";

What the problem? Can help me please? sorry for late update code :)

Upvotes: 1

Views: 408

Answers (2)

cadmy
cadmy

Reputation: 535

This piece of code name.indexOf(" (Gagal Berbayar)") returns you -1.

And negative indices are not allowed in substring method. So this is a root cause.

Upvotes: 0

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

In the if clause you're first checking whether vo.getName() contains your string (Gagal Berbayar) but then inside the block that executes if the if evaluates to true, you work with a different expression name instead of vo.getName(). There is no guarantee that name contains your string so this may fail.

You can fix that like this.

if (vo.getName().indexOf("(Gagal Berbayar)") > 0) {
  name = vo.getName().substring(0, vo.getName().indexOf(" (Gagal Berbayar)")) 
      + " (Gagal Berbayar)";
  System.out.println("Hasilnya 1: "+name);
}

However what if someone just enters (Gagal Berbayar) without a space in front of it? Then your if statement evaluates to true but the indexOf inside the block returns -1 because it can't find (Gagal Berbayar) (with a space in front of it).

So you should change that too:

    String GAGAL = " (Gagal Berbayar)";

    if (vo.getName().indexOf(GAGAL) > 0) {
      name = vo.getName().substring(0, vo.getName().indexOf(GAGAL)) + GAGAL;
      System.out.println("Hasilnya 1: "+name);
    } else {
      if (name.indexOf(GAGAL) > 0 ) {
        name = name.substring(0, name.indexOf(GAGAL));
        System.out.println("Hasilnya2 : "+name);
      }
    }

Upvotes: 1

Related Questions