Hashtable problem

Hi

I have a small problem with hashtable. I have wirtten one hashtable method and put 2 values into hashtable.

Now i want to call the method and return 2 values from the method.

i dont know how to return the values from the method.

here i put the code.

-

import java.io.File;

import org.w3c.dom.Document;

import org.w3c.dom.*;

import java.util.*;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

publicclass XMLFile{

public Hashtable xmlData()

{

String repname="";

String magicname ="";

try{

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (new File("magicFileMap.xml"));

NodeList children = doc.getElementsByTagName("DATAMAP");

Node child = children.item(0);

if(child !=null)

{

NamedNodeMap atts = child.getAttributes();

Node report = atts.getNamedItem("REPORTNAME" );

System.out.println("Report name:\t" + report.getNodeValue());

repname = report.getNodeValue();

Node report1 = atts.getNamedItem("MAGICNAME" );

System.out.println("MagicFile name:\t" + report1.getNodeValue() );

magicname = report1.getNodeValue();

}

Hashtable hashtable =new Hashtable ( ) ;

hashtable.put ("2",repname ) ;

hashtable.put ("1",magicname ) ;

Enumerationenum = hashtable.keys ( ) ;

String values="";

int i=1;

while ( enum.hasMoreElements ( ) ){

values = ( String ) enum.nextElement ( ) ;

System.out.println (" "+i+" ) key ="+values+", value ="+hashtable.get ( values ) ) ;

i++;

}

return hashtable;

}// End of try

catch(Exception e){

System.out.println("Value Error"+e.getMessage());

e.printStackTrace();

}

}

publicstaticvoid main ( String args[]){

XMLFile obj =new XMLFile();

System.out.println("Hai");

obj.xmlData();

}

}

Please any one help me how can i get the values from the hashtable.

Thanks

Merlin Roshina

[3966 byte] By [MerlinRosinaa] at [2007-11-26 16:31:23]
# 1
Hi,What java version are you using? You should try to avoid using Enumeration if you can.It's also a bit hard to understand what you mean. It looks like you are returning the whole Hashtable. Isn't that what you want to do?Kaj
kajbja at 2007-7-8 22:55:54 > top of Java-index,Java Essentials,Java Programming...
# 2
Eh, sorry, what is your problem? "How to return two values from a method"? Return either an instance of a custom container class, or an Object[2].By the way, don't call Enumerations "enum" - bad idea, because it's a keyword in Java 5.
CeciNEstPasUnProgrammeura at 2007-7-8 22:55:54 > top of Java-index,Java Essentials,Java Programming...
# 3

My guess is that the compiler is complaining because sometimes your code doesn't return anything. Is this correct?

If so, it would happen because there's a path through the method such that an exception is thrown before the return statement is reached. Control then goes to the catch block, which doesn't return anything. Control then passes past the catch block, where there is still no return statement.

Some advice:

1) For God's sake indent your code consistently and sanely. It's a lot easier to program if the structure of the text on the screen matches the structure of the code itself.

2) Don't catch Exception. Catch only the specific exceptions that can be thrown by the methods you invoke.

3) Ask yourself: if an exception is thrown, what should you return? What should be in the hashtable, or should there be a hashtable at all, if there's a problem? Maybe the simplest thing is to add "return null;" at the end of the method, and a caller can take a null return value as an indication of error. Or maybe something else should happen. You have to make this judgement.

4) Write incrementally. Write the smallest possible program that does a little bit of what you need, and get it working. Then add a little more. Use dummy code (compilable and runnable code that doesn't do what you need ultimately but models it a bit) if you have to. But don't move onto the next step until you have the current one working. It's a real pain to write fifty lines of code and then find out that there's some unknown problem somewhere in the middle of it.

paulcwa at 2007-7-8 22:55:54 > top of Java-index,Java Essentials,Java Programming...
# 4

> Eh, sorry, what is your problem? "How to return two values from a method"? Return

> either an instance of a custom container class, or an Object[2].

Hey good point.

@OP: if your goal here is to return a pair of values, then chances are you should be returning a class (which you define) that encapsulates the two values. Sticking them in a hashtable is a poor design, especially using numbers for keys like that.

paulcwa at 2007-7-8 22:55:54 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi

Thanks to all

here i put my xml file also. but i need to get the 2 values which are available in hashtable.

<?xml version="1.0" encoding="UTF-8"?>

<BASE_MAP>

<DATAMAP DATEMOD="19/01/2007 20:48:28" DESIGNERVER="3.5" MAGICNAME="AHfc381e794c80781ba80159949b64231d.jsp" RALIAS="1" REPORTNAME="smilereport.ivwr" REPORTTYPE="freeform" STATUS="A"/>

</BASE_MAP>

Thanks

MerlinaRoshin

MerlinRosinaa at 2007-7-8 22:55:54 > top of Java-index,Java Essentials,Java Programming...
# 6
@Op. Explain the problem? What is working? What isn't working? Why isn't it working? What error(s) do you get? When do you get them?Kaj
kajbja at 2007-7-8 22:55:54 > top of Java-index,Java Essentials,Java Programming...