gpt4 book ai didi

java - 将变量中的值与文本字段的值进行比较

转载 作者:行者123 更新时间:2023-12-01 08:07:18 25 4
gpt4 key购买 nike

我在类X中有一个变量,我想将其内容与另一个类Y中的字段值进行匹配。
我在类 X 中有以下代码:

String Cellresult;
Cell a1 = sheet.getCell(columNumber,rowNumber); // gets the contents of the cell of a an excell sheet
Cellresult = a1.getContents(); // stores the values in the variable named Cellresult
System.out.println(Cellresult); // prints the values which is working fine till here

现在在另一个类 Y 中,我想将 Cellresult 的值与具有 id txtVFNAME 的已填充字段的值进行比较.

我尝试过:

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

};

甚至在编译之前我就收到错误::Cellresult 无法解析为变量
我正在使用 java、Eclipse、IE 10、win 8。请帮忙。非常感谢。

最佳答案

您不能只在Y 类中引用X 类的变量。为了能够访问类 X 的实例变量,请创建一个使用它来访问它的实例。

X x = new X();
if (a.equals(x.Cellresult)){ // Cellresult is public

但在您的情况下,Cellresult 似乎存在于方法内部,而不是作为实例变量。在这种情况下,请从该方法返回您的 Cellresult 并在此处使用它。

X x = new X();
if (a.equals(x.methodThatReturnsCellresult())){ // methodThatReturnsCellresult is public

X 中的方法如下所示。

public String methodThatReturnsCellresult() {
// Other stuffs too.
String Cellresult;
Cell a1 = sheet.getCell(columNumber,rowNumber); // gets the contents of the cell of a an excell sheet
Cellresult = a1.getContents(); // stores the values in the variable named Cellresult
System.out.println(Cellresult);
return Cellresult; // returning the Cellresult value to be used elsewhere, in your case, in Class Y
}

关于java - 将变量中的值与文本字段的值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20652749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com