我创建了一个程序,它将控制台输出与excel单元格数据匹配并打印" TRUE" - "错误"基于数据匹配。问题是我的数据在这里匹配100%然后它也是打印" FALSE"在excel,为什么?
代码如下:
//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();
}
}
答案 0 :(得分:1)
标题和数据是两个不同的对象,意味着持有两个不同的引用。您的==符号将匹配引用而不是精确值。请改用equals()。
使用
if(title.equals(data))
而不是
if(title==data)