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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:39:33  来源:igfitidea点击:

.length cannot be resolved or is not a field

javastring

提问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 DataInputStreamis deprecated.

首先,不推荐使用readLine()in方法DataInputStream

Second, this method returns a String, which doesn't have a fieldlength. It only has the method length(). lengthis a property of arrays.

其次,此方法返回 a String,它没有 a fieldlength。它只有方法length()length是数组的属性。

回答by Jens

lengthis 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;
        }
    }   
}