[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.3

array is not printed on the terminal [problem with code]

Asked by perdoname_ in Java Programming Language, Programming Languages

Tags: Java

Hello Experts,

Im trying to run the code below as to parse the xml and then print the elements on the terminal as 2D array but instead of that it returns :
type: floor
x: 18
y: 14
renderhint
tile
type: wall
x: 19
y: 14
...
...


What can i do as to load the walls and other things of map instead of that ???

Thanks in advance!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
 
import javax.swing.text.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.Node;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
 
 
class LODServer {
     
      ServerBase<ClientHandler> server = null;
 
      /*
       * * Once the given number of players has connected, * the instance of
       * ServerBase<ClientHandler> returns * an array of them.
       */
      ArrayList<ClientHandler> clients = null;
 
      /*
       * * This is the data that encodes the state of the game. * The labyrinth is
       * stored in a 2D array with * numbers corresponding to the contents of each
       * location.
       */
      static final int TREASURE = 1;
      /* Larger positive numbers are several lots of treasure */
      static final int EMPTY = 0;
      static final int HEALTH = -1;
      static final int LANTERN = -2;
      static final int SWORD = -3;
      static final int ARMOUR = -4;
      static final int EXIT = -5;
      static final int WALL = -6;
 
      int mapWidth = 5;
      int mapHeight = 5;
      int map[/* mapHeight */][/* mapWidth */] = {
                  { WALL, WALL, ARMOUR, WALL, WALL },
                  { WALL, EXIT, EMPTY, TREASURE, TREASURE },
                  { LANTERN, TREASURE, EMPTY, HEALTH, LANTERN },
                  { WALL, SWORD, EMPTY, ARMOUR, TREASURE },
                  { WALL, WALL, SWORD, WALL, WALL } };
 
      boolean gameStarted = false;
      boolean gameFinished = false;
      int goal = 2; // The amount of treasure to collect
 
      /*
       * * The server has to be able to wait until the end of the player's turn. *
       * It uses a semaphore to do this. * The player acquires the semaphore at
       * the start of the turn * and releases it at the end of the turn. * This
       * allows the server to wait until the end of the player's turn by * simply
       * waiting for the semaphore to be released.
       */
      Semaphore playerTurn = new Semaphore(1);
 
      public void setup(int port, int players, String map) throws java.io.IOException {
	/*
            // Limit to one player
            if (players > 1) {
                  System.err
                              .println("The current implementation is limited to one player.");
                  players = 1;
            }
	*/
 
            // Create the object to handle the incoming connections
            // ( you don't have to understand how this works )
            // If the last arguement is true then all incoming and outgoing messages
            // to the instances of ClientHandler will be logged.
            // Set to false if you don't want this.
            server = new ServerBase<ClientHandler>(ClientHandler.class, players,
                        port, true);
            xmlParser(map);
      } // void setup (int port, int players, String map)
 
    
      Document document = null;
 
      public void readMap() {
 
//root
org.w3c.dom.Element map = document.getDocumentElement();
 
//title
String title = map.getAttribute("name");
System.out.println("name: "+title);
 
//goal
String goal = map.getAttribute("goal");
System.out.println("goal: "+goal);
 
//width & height
String width = map.getAttribute("width");
String height = map.getAttribute("height");
System.out.println("dimensions: "+width+"x"+height);
 
//children
NodeList mapChildren = map.getChildNodes();
for(int i=0;i<mapChildren.getLength();i++)
{
    Node mapChild = mapChildren.item(i);
    NamedNodeMap mapChildAttr = mapChild.getAttributes();
 
    //random-item
    if("random-item".equals(mapChild.getNodeName()))
    {
        System.out.println("random-item");
 
        //amount
        String amount = mapChildAttr.getNamedItem("amount").getNodeValue();
        System.out.println("amount: "+amount);
 
        //type
        String type = mapChildAttr.getNamedItem("type").getNodeValue();
        System.out.println("type: "+type);
    }
    //tile
    else if("tile".equals(mapChild.getNodeName()))
    {
        System.out.println("tile");
 
        //startlocation
        Node start = mapChildAttr.getNamedItem("startlocation");
        if(start != null)
        {
            String startlocation = start.getNodeValue();
            System.out.println("startlocation: "+startlocation);
        }
 
        //type
        String type = mapChildAttr.getNamedItem("type").getNodeValue();
        System.out.println("type: "+type);
 
        //x
        String tx = mapChildAttr.getNamedItem("x").getNodeValue();
        System.out.println("x: "+tx);
 
        //y
        String ty = mapChildAttr.getNamedItem("y").getNodeValue();
        System.out.println("y: "+ty);
 
        //children
        NodeList tileChildren = mapChild.getChildNodes();
        for(int j=0;j<tileChildren.getLength();j++)
        {
            Node tileChild = tileChildren.item(j);
            NamedNodeMap tileChildAttr = tileChild.getAttributes();
 
            //item
            if("item".equals(tileChild.getNodeName()))
            {
                System.out.println("item");
 
                //type
                Node ntype = tileChildAttr.getNamedItem("type");
                if(ntype != null)
                {
                    String itype = ntype.getNodeValue();
                    System.out.println("type: "+itype);
                }
            }
            //renderhint
            else if("renderhint".equals(tileChild.getNodeName()))
            {
                System.out.println("renderhint");
            }
        }
    }
}
      }
 
    public void xmlParser(String xmlMap)
        throws IOException
    {
        try
        {
            //parse settings
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setValidating(false);
            dbFactory.setIgnoringElementContentWhitespace(true);
 
            //parse document
            DocumentBuilder builder = dbFactory.newDocumentBuilder();
            document = builder.parse(new File(xmlMap));
 
            //create tmp map
            File tmpFile = File.createTempFile("__tmp__", "");
            File dtdFile = new File("map.dtd");
            Result result = new StreamResult(tmpFile);
 
            //transform xml
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFile.getAbsolutePath());
            transformer.transform(new DOMSource(document), result);
 
            //parse document again
            dbFactory.setValidating(true);
            builder = dbFactory.newDocumentBuilder();
            document = builder.parse(tmpFile);
        }
        catch(TransformerConfigurationException e)
        {
            System.out.println(e);
        }
        catch(TransformerException e)
        {
            System.out.println(e);
        }
        catch(ParserConfigurationException e)
        {
            System.out.println("error with new document builder");
        }
        catch(org.xml.sax.SAXException e)
        {
            System.out.println("Error with parser file, xml file does not conform to map DTD");
            System.out.println(e);
            System.out.println("Exiting game");
            System.exit(1);
        }
 
        //... process document ...
        readMap();
    }
 
      public void play() throws InterruptedException {
 
            // Wait for all of the clients to connect
            clients = server.getConnections();
            System.out.println(" done.");
 
            // Any set up for the game that is specific
            // to the clients goes here.
 
            // Give each ClientHandler a reference to this object
            // and thus access to the game data
            for (ClientHandler c : clients) {
                  c.server = this;
            }
 
            // Fix the player's starting location
            clients.get(0).x = 2;
            clients.get(0).y = 2;
 
            // Let each client know the amount of treasure it needs
            for (ClientHandler c : clients) {
                  c.serverGoal(goal);
            }
 
            // Play the game!
            System.out.println("Starting the game.");
            gameStarted = true;
            while (gameFinished == false) {
 
                  // Check that there is at least one player connected
                  if (clients.size() <= 0) {
                        System.out.println("Game abandoned");
                        break;
                  }
 
                  // Each player takes a turn
                  // for (ClientHandler c : clients) {
                  ClientHandler c = clients.get(0);
 
                  // Start turn
                  System.out
                              .println("Starting the turn of player \"" + c.name + "\"");
                  c.startTurn(); // During this, c will acquire() the playerTurn
                                          // semaphore
 
                  // Wait for the player to finish their turn
                  // (they will release() the semaphore at the end of their turn)
                  try {
                        playerTurn.acquire();
                  } catch (InterruptedException e) {
                        // This shouldn't happen in normal operation
                        // If it happens, it is because this code
                        // has been used in a larger bit of software
                        // thus the wrapper can deal with this condition
                        throw (e);
                  }
                  System.out.println("End of turn");
 
                  // Check to see if the current player has won
                  // (gameFinished is set to true in ClientHandler.clientEndTurn())
                  if (gameFinished == true) {
 
                        System.out.println("Game over, \"" + c.name
                                    + "\" is the winner");
 
                        // Tell the player they have won
                        c.serverMessage("Congratulations " + c.name + " you won!");
                        c.serverWin();
 
                        // Tell everyone else that they haven't
                        for (ClientHandler d : clients) {
 
                              if (d != c) {
                                    d.serverMessage("Sorry " + d.name + " it looks like "
                                                + c.name + " beat you.");
                                    d.serverLose();
                              }
 
                        }
 
                  }
 
                  // Release the semaphore so that the next player can start their
                  // turn
                  playerTurn.release();
                  // }
 
            }
 
            return;
 
      }
 
      // A simple helper function
      public static void printUsageAndDie(String error) {
            System.err.println(error);
            System.err.println("Usage:");
            System.err.println("\tjava LODServer <port> <players> <map>");
            System.exit(1);
      }
 
      // The main method just handles the command line arguements
      // creates one instance of LODServer and starts it running
      public static void main(String args[]) throws java.io.IOException {
 
            // Check the command line arguements
            if (args.length != 3) {
                  printUsageAndDie("Incorrect number of command line arguements");
            } else {
                  // Parse the command line arguements
                  int port = Integer.parseInt(args[0]);
 
                  // Only ports 1025 to 65535 are suitable
                  if ((port <= 1024) || (port >= 65536)) {
                        printUsageAndDie("Invalid port number");
                  }
 
                  int players = Integer.parseInt(args[1]);
                  if (players < 0) {
                        printUsageAndDie("Invalid number of users");
                  }
 
                  // Create a LODServer object to run the game
                  LODServer s = new LODServer();
 
                  // Set it up
                  s.setup(port, players, args[2]);
 
                  // Go!
                  try {
                        s.play();
                  } catch (InterruptedException e) {
                        // Something has gone wrong - abort
                        System.err.println("Caught InterruptedException(?)");
                        System.exit(1);
                  }
            }
      }
}
[+][-]03/13/08 07:39 AM, ID: 21116620Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Programming Language, Programming Languages
Tags: Java
Sign Up Now!
Solution Provided By: ADSLMark
Participating Experts: 1
Solution Grade: A
 
[+][-]03/12/08 12:24 PM, ID: 21109751Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/08 12:30 PM, ID: 21109809Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 12:30 PM, ID: 21109812Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 12:35 PM, ID: 21109859Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/08 12:43 PM, ID: 21109936Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 12:52 PM, ID: 21110023Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/08 01:24 PM, ID: 21110367Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 01:33 PM, ID: 21110442Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/08 01:41 PM, ID: 21110535Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 02:06 PM, ID: 21110770Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/08 02:07 PM, ID: 21110783Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 02:16 PM, ID: 21110856Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/08 02:25 PM, ID: 21110929Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/12/08 08:23 PM, ID: 21113093Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 01:59 AM, ID: 21114307Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/13/08 04:18 AM, ID: 21114996Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 04:22 AM, ID: 21115010Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/13/08 05:07 AM, ID: 21115252Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 06:21 AM, ID: 21115876Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/13/08 06:34 AM, ID: 21115983Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 06:44 AM, ID: 21116073Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/13/08 06:49 AM, ID: 21116129Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 06:58 AM, ID: 21116237Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/13/08 07:09 AM, ID: 21116370Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 07:49 AM, ID: 21116728Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/13/08 07:53 AM, ID: 21116773Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/13/08 07:59 AM, ID: 21116850Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 / EE_QW_2_20070628