Jump to content

IF statement is not working for me

Recommended Posts


 		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 by newcoder1010
Link to post
Share on other sites

I would start by printing the return value of shouldLoginNo.trim(). It clearly is not equal to "Yes".

Link to post
Share on other sites

Print the variable to see if it contains the value you expected it to have.

System.out.println("*" + shouldLoginNo + "*");


Link to post
Share on other sites

Can you show the full code including the section where the data is retrieved from the spreadsheet?

Link to post
Share on other sites

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 post
Share on other sites

There may be invisible characters. What number do you get when you call shouldLoginNo.length() ?

Link to post
Share on other sites

The == was probably checking that both strings were pointing to the same space in memory, which they probably were not.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...