newLearner
newLearner

Reputation: 184

Unable to compare values

I have a class named "Read" where I am reading the values of a specific cell A1 of an Excel sheet and then putting that value in a variable "Cellresult". In another class "Patient" I am trying to compare the value of a text field with id "txtVFNAME" against the variable I just created for the specific cell. I am doing as below for that cell reading and variable storage (it's working fine).

public class Read {

public String Cellresult;

public static void main(String[] args) {

    Workbook workbook = null;
    Sheet sheet = null;
    String Cellresult;

        try {
            workbook = Workbook.getWorkbook(new File("D:\\rptViewer.xls"));
        } catch (BiffException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sheet = workbook.getSheet(0);

        int columNumber = 0;
        int rowNumber = 1;

        Cell a1 = sheet.getCell(columNumber,rowNumber);
        Cellresult = a1.getContents();
        System.out.println(Cellresult);

        if(a1.getContents().equalsIgnoreCase("HAJI NIGAR")){
            System.out.println("Found it");
        }       
    }  
}

For comparing values, I tried below in the class "Patient", but nothing printed:

Read compare = new Read ();
WebElement a = idriver.findElement(By.id("txtVFNAME"));
if (a.equals(compare.Cellresult)){
     System.out.println("text is equal");
};

I am using Java, IE 10, Win 8, Eclipse.

Upvotes: 0

Views: 81

Answers (2)

Qerf
Qerf

Reputation: 11

just remove your String Cellresult;

in your main you currently use Cellresult = a1.getContents();

This currently reffers to your local variable CellResult. In your compare part, you refer to the property Cellresult which is not the same. You have set a value of your local variable. Your property remains empty, resulting in a compare method that compares with nothing

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209102

You have two different String Cellresult;s declared. The one inside the main is the one that will get initialized with a value, but the class member Cellresult is still null. So when you try and access it from another class, it has no value.

Maybe delete the one inside and make the outer one static

Upvotes: 2

Related Questions