gpt4 book ai didi

java - java中如何与字符串进行比较?

转载 作者:行者123 更新时间:2023-12-01 18:32:35 24 4
gpt4 key购买 nike

嗨,我正在从文件中读取数据,然后我想比较该字符串与我的字符串我该字符串等于我的字符串,然后运行 ​​if 条件,否则 else 条件

My.jsp

   <%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ page import="java.io.*" %>
<html>
<body>


<%
String s1="1";
// int i=4;
// String s=null;
// String s2="1";
String s3=null;
String file = application.getRealPath("/") + "data.txt";
BufferedReader reader = new BufferedReader(new FileReader(file));

String line;
while((line = reader.readLine())!= null){

s3=line.toString();
}

if(s3.equalsIgnoreCase(s1))
%>
<%@ include file="MarketWiseData.jsp" %>
<%else
{%>
<%@ include file="CommodityWise.jsp" %>
<%
}
%>

</body>
</html>

我怎样才能达到我的目标?

提前致谢

最佳答案

阅读您的代码:

while((line = reader.readLine()) != null) {
// sb.append(line+"\n");
out.println(line);
}

if (line.equalsIgnoreCase(s1))
out.println("Shkati");
else {
out.println("sharma");
}

line 不为空时,您有一个 while 循环继续运行。因此,当 line 为 null 时,它会停止。因此,在 if 测试中,line 保证为 null。因此,测试一定会抛出 NullPointerException,因为在 null 上调用方法没有意义。

请注意,如果您的代码如上面的代码所示正确缩进,您将能够立即看到其结构和逻辑。未能缩进代码会让您感到困惑和不清楚。

关于java - java中如何与字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23466313/

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