1

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 :)

3
  • I think you just simply need to substracl 1 at indexof. Use this: substring(0, name.indexOf(" (Gagal Berbayar)")-1) Commented Oct 3, 2014 at 8:59
  • Please simplify your code example and show some example inputs that trigger the problem. There is a lot of code we don't need to see there - just focus on the substring stuff and demonstrate the problem. Commented Oct 3, 2014 at 9:03
  • Which line throws the error? Commented Oct 3, 2014 at 9:08

2 Answers 2

1

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);
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I also think the same. But what if there is a space in name. For example - "asdf sf".indexOf("sf") and "asdf sf".indexOf(" sf"). I think @user3562918 should clarify what name actually contains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.