简体   繁体   中英

using a String variable outside loop

inside a StAX parsing example, I am setting a 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 . You will see it being attempted 5 other spots going down into the code - all report the same. The first line within the if statement works.

How can I use the string "dataread" elsewhere in my program?

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.

Try to declare after 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.

For that reason, you will not be able to access this variable outside the if block. To allow access to this variable when you print it, you must declare it before the if statement. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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