简体   繁体   English

在循环外使用String变量

[英]using a String variable outside loop

inside a StAX parsing example, I am setting a String dataread = se.getElementText(); 在StAX解析示例中,我设置了String dataread = se.getElementText(); . I am successfully printing that string (dataread) immediately. 我成功地立即打印了该字符串(数据读取)。

However, everywhere else in my code, it won't work, it reports that it cannot find the symbol dataread . 但是,在我的代码的其他任何地方,它都行不通,它报告找不到符号dataread You will see it being attempted 5 other spots going down into the code - all report the same. 您将看到它正被尝试进入代码的其他5个位置-所有报告都相同。 The first line within the if statement works. if语句中的第一行有效。

How can I use the string "dataread" elsewhere in my program? 如何在程序的其他位置使用字符串“ dataread”?

Thanks 谢谢

public static void main(String[] args) throws FileNotFoundException, XMLStreamException
{
    // TODO code application logic here
    //System.out.println("Here");
    //String filename = null;
    String filename = "BookCatalog2.xml";
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(new FileReader(filename));
    while(reader.hasNext())
            {
                XMLEvent event = reader.nextEvent();
                XMLEvent nextEvent = reader.peek();
                switch (event.getEventType())
                        {
                    case XMLEvent.START_ELEMENT:

                        StartElement se = event.asStartElement();
                        //System.out.println("Here");
                        //System.out.print("<" + se.getName());

                        //System.out.print(" " + se.getName());
                        //System.out.printf("\n");
                        String elem = se.getName().toString();
                        //String elem = "1";
                        //System.out.printf("elem = %s\n",elem);
                        //String ele = event.getAttributeName();

                        System.out.printf("event = %s\n",se.getName());

                        //if( se.getName().toString() == "{http://www.publishing.org}Date")
                       if( se.getName().toString().equals("Date"))
                       //if( elem == "1")
                        {
                            System.out.println("Here !!!!!!!!!!!!!!!!!");
                            String dataread = reader.getElementText();
                            //System.out.printf("data = %s\n",reader.getElementText());
                            System.out.printf("data = %s\n",dataread);
                        }
                        Iterator attributes = se.getNamespaces();
                        System.out.printf("DATA READ = %s\n",dataread);
                        while(attributes.hasNext())
                        {
                            Attribute attr= (Attribute)attributes.next();
                            System.out.print(" " + attr.getName() + "=\"" +attr.getValue() +"\"");
                            System.out.printf("\n");
                        }//end while loop
                    System.out.print(">");
                        if(nextEvent.isCharacters())
                        {
                            Characters c = reader.nextEvent().asCharacters();
                            if(!c.isWhiteSpace())
                            System.out.print(c.getData());
                            System.out.printf("\n");


                        }// end if
                        System.out.printf("DATA READ = %s\n",dataread);
                    /*case XMLEvent.END_ELEMENT>
                        EndElement ee = event.asEndElement();
                        System.out.print("</"+ee.getName()+">");
                        break;
                        * */
                        System.out.printf("DATA READ = %s\n",dataread);
                        }// end witch
                System.out.printf("DATA READ = %s\n",dataread);
            }// end while
    System.out.printf("DATA READ = %s\n",dataread);
    reader.close();
}//end Main
}// public claSS

Your string is within the scope of your first IF, you must declare it at the beginning of the method. 您的字符串在第一个IF的范围内,必须在方法开始时声明它。

Try to declare after String filename = "BookCatalog2.xml"; 尝试在String filename = "BookCatalog2.xml";之后声明String filename = "BookCatalog2.xml";

Ex: 例如:

String filename = "BookCatalog2.xml";
String dataread = "";

Your String variable dataread is a local variable that has a scope within the if clause ONLY. 您的String变量dataread是一个局部变量,其作用域仅在if子句中。

For that reason, you will not be able to access this variable outside the if block. 因此,您将无法在if块之外访问此变量。 To allow access to this variable when you print it, you must declare it before the if statement. 要在打印时允许访问此变量,必须在if语句之前声明它。 eg: 例如:

String dataread = null;
if (test) {
    dataread = ...;
}

System.out.println(dataread); // etc

What you have to do is declare the local variable at a level where every method is at the same level or below it. 您要做的是在每个方法都处于相同级别或更低级别的级别声明局部变量。 As it is now you're declaring the variable in a if-clause, then you will only be able to reach it from that if-clause. 由于现在要在if子句中声明变量,因此只能从if子句中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM