0

I have created program which match console output with excel cell data and prints "TRUE" - "FALSE" based on data matching. Issue is my data is matching here 100% then also it is printing "FALSE" in excel , WHY?

Code is given below :

     //CODE TO REMOVE UNNECESSARY WARNING
    System.setProperty("org.apache.commons.logging.Log",
    "org.apache.commons.logging.impl.Jdk14Logger");


    //CALL FIREFOX DRIVER TO OPEN IT
    WebDriver driver = new FirefoxDriver();

    //GETTING PAGE , OPEN IT IN BROWSER AND GET PAGE TITLE TO PRINT IT
    driver.get("http://en.wikipedia.org/wiki/Software_testing");
    String title = driver.getTitle();
    System.out.println(title);


    //EXCEL FILE READING
    FileInputStream input = new FileInputStream("D:\\sel.xls");


    int count=0;

    HSSFWorkbook wb = new HSSFWorkbook(input);
    HSSFSheet sh = wb.getSheet("sheet1");
    HSSFRow row = sh.getRow(count);
    String data = row.getCell(1).toString();
    System.out.println(data);


    //CONDITION TO MATCH CONSOLE OUTPUT WITH EXCEL CELL DATA VALUE
    if(title==data)

    {
        FileOutputStream webdata = new FileOutputStream ("D:\\sel.xls");
        row.createCell(10).setCellValue("TRUE");
        wb.write(webdata);

    }
    else
    {
        FileOutputStream webdata = new FileOutputStream ("D:\\sel.xls");
        row.createCell(11).setCellValue("FALSE");
        wb.write(webdata);

    }

    driver.close(); 
    wb.close();
    input.close();


  }


  }
Helping Hands
  • 5,292
  • 9
  • 60
  • 127

1 Answers1

1

title and data are two different objects, means holding two different references. Your == symbol will match references and not exact value. Use equals() instead.

Use

if(title.equals(data))

instead of

if(title==data)
janasainik
  • 811
  • 5
  • 20
  • 40
  • Hey. Awesome. Yes its working. But why == not working. When we can use == ?? exactly? – Helping Hands Dec 04 '14 at 06:47
  • As said, if you simply wants to check the content then best bet is equals(this is bit costly). If sure about object reference, then go for ==. More information is on http://stackoverflow.com/questions/767372/java-string-equals-versus – janasainik Dec 04 '14 at 06:53
  • Thanks a lot. Got actual concept. – Helping Hands Dec 04 '14 at 06:55