0

In this below code, tried to create a 10 cells with String value which will be sequentially increased like "BRT_1", "BRT_2", "BRT_3"...."BRT_10". But in excel, only BRT_ is entered not the integer value concatenated with "BRT_".

        for(r=1;r<10;r++)
        {
            sh.createRow(r);
            for(c=0;c<body.size();c++)
            {
                String value = body.get(c).toString();
                if(value == "BRT_")
                {
                    sh.getRow(r).createCell(c).setCellValue(value+r);
                }
                else
                    sh.getRow(r).createCell(c).setCellValue(value);
            }
        }
Wasim
  • 1
  • 1
  • 3
    you must use the .equals method to compare two strings: if (value.equals("BRT_")) – jalynn2 Nov 18 '19 at 19:14
  • This is because `if(value == "BRT_")` will never be true since `==` is not the correct way to test for `String` equality. Correct way to do so is `if("BRT_".equals(value))`. – Axel Richter Nov 18 '19 at 19:14
  • Thanks @jalynn2..it's my mistake..Now it's working. – Wasim Nov 18 '19 at 19:39

1 Answers1

1

Replace

if(value == "BRT_")
{
    sh.getRow(r).createCell(c).setCellValue(value+r);
}

with

if("BRT_".equals(value))
{
    sh.getRow(r).createCell(c).setCellValue(value+String.valueOf(r));
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110