Benjamin Bolyard Ответов: 1

Как настроить arraylist<string>


Hi, 
I am trying to run this code and ArrayList<String> data = handler.getnames(); is showing an error variable data is alredy defined in method main(String[]). I am guessing it wants me to combine it with ArrayList<String> data = handler.getsetnums();. But how to i do it? or is there another way?   
Ones the code works, and should be able to run it and get an output, now when i only have one Array working the code does what it needs to do.

expected outcome

    <set-num>00-1</set-num>
    <name>WEETABIX CASTLE</name>

 

Thank you.



    ---MAIN.java---
    package assign6;
        
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.logging.Level;
        import java.util.logging.Logger;
        import org.xml.sax.InputSource;
        import org.xml.sax.SAXException;
        import org.xml.sax.XMLReader;
        import org.xml.sax.helpers.XMLReaderFactory;
        
        /**
         *
         * @author assign
         */
        public class Assign6 {
        
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                System.out.print("Welcome to Benjamin Bolyard's Lego Sorter");
                try {
                    XMLReader reader = XMLReaderFactory.createXMLReader();
                    MyHandler handler = new MyHandler();
                    reader.setContentHandler(handler);
                    reader.setErrorHandler(handler);
                    
                    InputSource inputSource = new InputSource("legoSets.xml");
                    reader.parse(inputSource);
                    
                    ArrayList<String> data = handler.getsetnums();
                    
                    System.out.println("setnum List");
                    System.out.println("----------");
                    for (int i = 0; i < data.size(); i++) {
                        String setnum = data.get(i);
                        setnum = setnum.toUpperCase();
                        System.out.println((i + 1) + ": " + setnum);
                    }
                    
                    ArrayList<String> data = handler.getnames();
                    
                    System.out.println("name List");
                    System.out.println("----------");
                    for (int i = 0; i < data.size(); i++) {
                        String name = data.get(i);
                        name = name.toUpperCase();
                        System.out.println((i + 1) + ": " + name);
                    }
                    
                } catch (SAXException ex) {
                    Logger.getLogger(BolyardAssign6.class.getName())
                            .log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(BolyardAssign6.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
            }
            
        }
    --HANDLER.java---
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package bolyardassign6;
    
    import java.util.ArrayList;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
    
    /**
     *
     * @author bolya
     */
    public class MyHandler extends DefaultHandler {
    
        private boolean inlegosets;
        private boolean inname;
        private boolean insetnum;
        private String nameBuffer;
        private String setnumBuffer;
        
         private ArrayList<String> setnums, names;
        
        public ArrayList<String> getsetnums() {
            return setnums;
        
        }
        
        public ArrayList<String> getnames() {
            return names;
        }
    
        @Override
        public void startDocument() throws SAXException {
            // System.out.println("Start Document")
            insetnum = false;
            inname = false;
            inlegosets = false;
            nameBuffer = "";
            names = new ArrayList<>();
        }
    
        @Override
        public void startElement(String uri, String localName,
                String qName, Attributes attributes) throws SAXException {
            if (localName.equals("lego-sets")) {
                inlegosets = true;
            } else if (inlegosets && localName.equals("setnum")) {
                // System.out.println("Start element : " + localName);
                insetnum = true;
                setnumBuffer = "";
            
            } else if (inlegosets && localName.equals("name")) {
                // System.out.println("Start element : " + localName);
                inname = true;
                nameBuffer = "";
            }
        }
    
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            String data = new String(ch, start, length);
            if (inlegosets && inname) {
                // System.out.println("Characters : " + data);
                nameBuffer = nameBuffer + data;
            } else if (inlegosets && insetnum) {
                // System.out.println("Characters : " + data);
                setnumBuffer = setnumBuffer + data;
        }
        }
    
        @Override
        public void endElement(String uri, String localName,
                String qName) throws SAXException {
            if (localName.equals("inlegosets")) {
                inlegosets = false;
            } else if (inlegosets && localName.equals("setnum")) {
                // System.out.println("Title: " + titleBuffer);
                // System.out.println("End element   : " + localName);
                // System.out.println();            
                setnums.add(setnumBuffer);
                insetnum = false;
            } else if (inlegosets && localName.equals("name")) {
                // System.out.println("Title: " + titleBuffer);
                // System.out.println("End element   : " + localName);
                // System.out.println();            
                names.add(nameBuffer);
                inname = false;
            }
        }
    
        @Override
        public void endDocument() throws SAXException {
            // System.out.println("End Document");
        }
    
        @Override
        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR: Line number " + e.getLineNumber());
            System.out.println("ERROR:             " + e.getMessage());
        }
    
        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL ERROR: Line number " + e.getLineNumber());
            System.out.println("FATAL ERROR:             " + e.getMessage());
        }
    
    }
    
    ---legoset.xml---
    <lego-sets>
        <set>
            <set-num>00-1</set-num>
            <name>WEETABIX CASTLE</name>
            <year>1970</year>
            <num-parts>471</num-parts>
        </set>
        <set>
            <set-num>0011-2</set-num>
            <name>TOWN MINI-FIGURES</name>
            <year>1978</year>
            <num-parts>12</num-parts>
        </set>
        <set>
            <set-num>0011-3</set-num>
            <name>CASTLE 2 FOR 1 BONUS OFFER</name>
            <year>1987</year>
            <num-parts>2</num-parts>
        </set>
        <set>
            <set-num>0012-1</set-num>
            <name>SPACE MINI-FIGURES</name>
            <year>1979</year>
            <num-parts>12</num-parts>
        </set>
    </lego-sets>


Что я уже пробовал:

Примеры книг и примеры w3school.

1 Ответов

Рейтинг:
5

Benjamin Bolyard

You have the right idea. The error is caused because you have two variable declarations withe same name:

<pre>ArrayList data = handler.getnames()
and

ArrayList data = handler.getsetnums();
Java doesn't allow this. However, you don't necessarily want to "combine them together" because these are two separate lists that seem to represent two different things.

I suggest changing the names of both variables to something more meaningful than data. For example:

ArrayList names = handler.getnames()
and

ArrayList setnums = handler.getsetnums();
Presumably, names and setnums represent something meaningful for what you are doing here.


кредит на
Code-Apprentice @stackoverflow<pre>