newcoder1010 Posted May 13, 2020 Share Posted May 13, 2020 (edited) Hello, String msg =row.getCell(5).toString(); System.out.println("msg " + msg); if (shouldLoginNo.trim() == "Yes"){ System.out.println("msg " + msg); } Its not printing the IF message. msg is coming from excel spreadsheet. It is getting the value "Yes" just fine. I added trim method but it is still not printing IF message. If I hard code value like this and it works. It tells me something is wrong when it reads value from excel. shouldLoginNo = "Yes"; How can I print the msg inside the IF block? Thanks! Edited May 13, 2020 by newcoder1010 Link to comment Share on other sites More sharing options...
Ingolme Posted May 13, 2020 Share Posted May 13, 2020 I would start by printing the return value of shouldLoginNo.trim(). It clearly is not equal to "Yes". Link to comment Share on other sites More sharing options...
newcoder1010 Posted May 13, 2020 Author Share Posted May 13, 2020 shouldLoginNo here is a varriable. Not sure how to return value? I also updated my original post. It works when i have hard coded value to the variable. Link to comment Share on other sites More sharing options...
Ingolme Posted May 13, 2020 Share Posted May 13, 2020 What data type is that variable? Where did it come from? Link to comment Share on other sites More sharing options...
newcoder1010 Posted May 13, 2020 Author Share Posted May 13, 2020 String is the data type. It reads data from excel cell. Link to comment Share on other sites More sharing options...
Ingolme Posted May 13, 2020 Share Posted May 13, 2020 Print the variable to see if it contains the value you expected it to have. System.out.println("*" + shouldLoginNo + "*"); Link to comment Share on other sites More sharing options...
newcoder1010 Posted May 13, 2020 Author Share Posted May 13, 2020 I get this *Yes* Link to comment Share on other sites More sharing options...
Ingolme Posted May 13, 2020 Share Posted May 13, 2020 Can you show the full code including the section where the data is retrieved from the spreadsheet? Link to comment Share on other sites More sharing options...
newcoder1010 Posted May 13, 2020 Author Share Posted May 13, 2020 I am doing selenium web driver testing and to read data from excel, I am using apache POI. Again reading data from excel, I have no problem. When it compares in the IF statement, I have problem. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; File file = new File("C://Tek_AllSites.xls"); // Open the Excel file FileInputStream fis = new FileInputStream(file); // Access the required test data sheet HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet sheet = wb.getSheet("AllSites"); int TotalRow; int TotalCol; String pageTitle = ""; TotalRow = sheet.getLastRowNum(); System.out.println("Total row " + TotalRow); TotalCol = sheet.getRow(0).getLastCellNum(); HSSFRow headerRow = sheet.getRow(0); String result = ""; for(int count = 1;count<=TotalRow;count++){ HSSFRow row = sheet.getRow(count); String shouldLoginNo =row.getCell(5).toString(); System.out.println("pageTitle " + pageTitle); System.out.println("*" + shouldLoginNo + "*"); // Printig Yes just fine if (shouldLoginNo.trim() == "Yes"){ System.out.println("*" + shouldLoginNo + "*"); // Not printing here } } Link to comment Share on other sites More sharing options...
Ingolme Posted May 13, 2020 Share Posted May 13, 2020 There may be invisible characters. What number do you get when you call shouldLoginNo.length() ? Link to comment Share on other sites More sharing options...
newcoder1010 Posted May 13, 2020 Author Share Posted May 13, 2020 System.out.println("*" + shouldLoginNo + + shouldLoginNo.length() + "*"); I get this *Yes3* Link to comment Share on other sites More sharing options...
Ingolme Posted May 13, 2020 Share Posted May 13, 2020 See if shouldLoginNo.trim().equals("Yes") makes a difference. Link to comment Share on other sites More sharing options...
newcoder1010 Posted May 13, 2020 Author Share Posted May 13, 2020 Thank you so much! It has worked now. I am still not sure why it does not work with == operator. -) Link to comment Share on other sites More sharing options...
Ingolme Posted May 14, 2020 Share Posted May 14, 2020 The == was probably checking that both strings were pointing to the same space in memory, which they probably were not. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now