Hashtable returning null - help
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   saiful_hakim
Posted On:   Tuesday, November 18, 2003 02:49 AM

I am reading text file, put the value pair as hashtable, then try to retrieve element of hashtable using its key. However it returns null, when I tried to enumerate it everythings looks okay. Any comment will be appreciated. Below my code, and console out further below. // my code import java.io.BufferedReader; import java.io.FileReader; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; public class OpenParamSet { static Hashtable paramPair; static String hidtextSubTitle; public static void main(String[] args) { try { String filename = "C:/upload/test1023.txt"; FileReader fileReader = ne   More>>

I am reading text file, put the value pair as hashtable, then try to retrieve element of hashtable using its key. However it returns null, when I tried to enumerate it everythings looks okay. Any comment will be appreciated. Below my code, and console out further below.

// my code
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class OpenParamSet {
static Hashtable paramPair;
static String hidtextSubTitle;

public static void main(String[] args) {

try {

String filename = "C:/upload/test1023.txt";

FileReader fileReader = new FileReader(filename);
BufferedReader buffReader = new BufferedReader(fileReader);

String buffer = new String();
paramPair = new Hashtable();
while ((buffer = buffReader.readLine()) != null) {
System.out.println(buffer);
StringTokenizer st =
new StringTokenizer(buffer.toString(), "=");
if (st.countTokens() == 2) {

paramPair.put(st.nextToken(), st.nextToken());
}

}

buffReader.close();

Enumeration enum1 = paramPair.keys();
Enumeration enum2 = paramPair.elements();

while (enum1.hasMoreElements()) {
System.out.println(
" key = "
+ enum1.nextElement()
+ " with value = "
+ enum2.nextElement());

}

String hidselectXAxis = (String) paramPair.get("hidselectXAxis");
System.out.println("congrats hidtextSubTitle = " + hidselectXAxis);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

//Console out
hidtextSubTitle = Subtitle
hidselectObject = Project
hidselectXAxis = Name
hidtextFooter = Footer
submit = Save
hidselectSeries3 = OriginalBudget
hidselectTemplate = /PFCharts/servlet/Configure2?chartTemplateFile=barsaiful.cdl
hidselectSeries2 = OriginalBudget
hidselectSeries1 = OriginalBudget
hidtextTitle = Title
hidselectField = Name
hidselectOrdering = Desc
key = hidtextSubTitle with value = Subtitle
key = hidselectXAxis with value = Name
key = hidtextFooter with value = Footer
key = hidselectSeries3 with value = OriginalBudget
key = hidselectOrdering with value = Desc
key = submit with value = Save
key = hidselectObject with value = Project
key = hidselectSeries2 with value = OriginalBudget
key = hidtextTitle with value = Title
key = hidselectField with value = Name
key = hidselectSeries1 with value = OriginalBudget
congrats hidtextSubTitle = null

   <<Less

Re: Hashtable returning null - help

Posted By:   Eric_Maginniss  
Posted On:   Tuesday, November 18, 2003 07:30 AM

BTW, you might want to consider using the java.util.Properties class instead of handling all of the text parsing yourself.

Re: Hashtable returning null - help

Posted By:   saiful_hakim  
Posted On:   Tuesday, November 18, 2003 03:53 AM

Never mind, I should've used trim() to clean up whitespaces before putting into hashtable. Problem solved.
About | Sitemap | Contact