无法比较值

时间:2013-12-18 12:32:56

标签: java excel

我有一个名为"Read"的类,我正在读取Excel工作表的特定单元格A1的值,然后将该值放入变量"Cellresult"中。在另一个类"Patient"中,我试图将文本字段的值与id "txtVFNAME"进行比较,而不是我为特定单元格创建的变量。 我正在进行如下的单元读取和变量存储(它工作正常)。

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");
        }       
    }  
}

为了比较数值,我在下面的"Patient"课程中尝试过,但没有打印出来:

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

我正在使用Java,IE 10,Win 8,Eclipse。

2 个答案:

答案 0 :(得分:2)

您声明了两个不同的String Cellresult;main中的一个是将使用值初始化的那个,但类成员Cellresult仍为null。因此,当您尝试从另一个类访问它时,它没有任何价值。

也许删除里面的那个并使外部的static

答案 1 :(得分:0)

只需删除您的String Cellresult;

即可

在您的主页中,您当前使用的是Cellresult = a1.getContents();

目前这是您的本地变量CellResult。在比较部分中,您引用的属性Cellresult不同。您已设置本地变量的值。您的属性仍为空,导致比较方法与任何内容进行比较

相关问题