java .length 无法解析或不是字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25236560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.length cannot be resolved or is not a field
提问by user23102
I have tried really hard to solve this with previous similar answers, but am still nto able to see my problem, hope you can help. My code looks like this:
我已经用以前的类似答案非常努力地解决了这个问题,但我仍然无法看到我的问题,希望你能帮忙。我的代码如下所示:
String MyContent =" ";
String nextline = " ";
InputStream in = new FileInputStream(f);
BufferedInputStream bin = new BufferedInputStream(in);
DataInputStream din = new DataInputStream(bin);
while(din.available()>1)
{
nextline = din.readLine();
//Filter out XML headers which are not browser compliant
if (nextline.length > 4)
{
if (nextline.substring(1,5) != "<?xml")
{
MyContent=MyContent+ nextline;
}
}
}
out.print (MyContent);
in.close();
bin.close();
din.close();
And I am getting an error:
我收到一个错误:
An error occurred at line: 25 in the jsp file: /MaxiSunReports/DisplayXMLFile.jsp
nextline.length cannot be resolved or is not a field
22: nextline = din.readLine();
23: nextline = "THISISATEST";
24: //Filter out XML headers which are not browser compliant
25: if (nextline.length > 4)
26: {
27: if (nextline.substring(1,5) != "<?xml")
回答by blueygh2
First, the method readLine()
in DataInputStream
is deprecated.
首先,不推荐使用readLine()
in方法DataInputStream
。
Second, this method returns a String
, which doesn't have a field
length
. It only has the method length()
. length
is a property of arrays.
其次,此方法返回 a String
,它没有 a field
length
。它只有方法length()
。length
是数组的属性。
回答by Jens
length
is not an field. It is a function, so you have to call nextline.length() > 4
length
不是一个字段。这是一个函数,所以你必须调用nextline.length() > 4
回答by Manish Kr. Shukla
length is not a property, it is a method..
长度不是属性,它是一种方法..
Use
利用
while(din.available()>1)
{
nextline = din.readLine();
//Filter out XML headers which are not browser compliant
if (nextline.length() > 4)
{
if (nextline.substring(1,5) != "<?xml")
{
MyContent=MyContent+ nextline;
}
}
}