-2
for ( int i = 0; i < s.getColumns(); i++ )
{
    if (s.getCell(i,0).getContents() == "Type" );
    {
         typeIndex = i;
         System.out.println(s.getCell(i,0).getContents());
    }
}

Basically I have an excel file, and a sheet declared as s I am using a for loop to iterate through all the columns of the sheet. I am looking for the heading called "Type" by searching each column heading using row 0, and then I want to store the column index wherever the column with "Type" heading is found, in the variable typeIndex When I try to do this, it always reads the if statement as true.

code explanation:

The if statement basically gets the column heading at the current ith iteration, and compares it to the string "Type". I am also printing the column heading each time, and they are not all "Type" , they are correctly iterating and showing the different column names in the excel sheet.

But it shouldn't even be entering the if statement then..? Please comment if you need further elaborating, tried to explain it the best I could.

Faaiz Haque
  • 25
  • 10
  • You should post complete code related to this question. Where is getCell method. – Khemraj Sharma Jul 23 '18 at 11:43
  • 7
    Remove the `;` after your if statement. Also have a look at [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Jul 23 '18 at 11:43
  • 4
    Compare `String`s with `.equals()`... And yes, remove the semicolon after the condition in your if statement. – deHaar Jul 23 '18 at 11:43
  • 1
    Possible duplicate of [Semicolon at end of 'if' statement](https://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement) – Ivar Jul 23 '18 at 11:53
  • Oops, didn't even notice the semicolon. No this wasn't meant to be a duplicate, didn't even realize it was there even though I spent like 2 hours staring at it. Thanks @OHGODSPIDERS – Faaiz Haque Jul 23 '18 at 11:58

1 Answers1

0
if (s.getCell(i,0).getContents() == "Type" )**";"** 

You should remove the semicolon after if statement

Gary
  • 13,303
  • 18
  • 49
  • 71
Nurhan
  • 87
  • 1
  • 6